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 を使用しており、「WWW」を「eee」の 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