Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在 txt 文件中找到一行,然后在找到的行上方插入字符串 3 行
输入:
aaa bbb ccc ddd eee fff
我想查找“eee”,然后在其上方打印“WWW”3 行。输出:
aaa WWW bbb ccc ddd eee fff
我正在使用 awk,只能在“eee”上方打印“WWW”1 行,而不是 3:
awk '/eee/{print "WWW"} 4' file.txt
有任何想法吗?
单程:
awk '{a[NR]=$0;}/eee/{a[NR-3]="www\n" a[NR-3];}END{for(i=1;i<=NR;i++)print a[i];}' file