加入SED连续两行 [英] Add two consecutive lines in sed
本文介绍了加入SED连续两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想第一个字符串搜索后添加两行。我使用的:
I would like to add two lines after the first string search. I am using:
$ cat file1
HAI
BYE
HAI
ONE
TWO
$ VAR=`cat -n file1 |grep -w HAI |head -1 |awk '{print $1}'`
$ sed "$VAR a\
LINE ONE \
LINE TWO
" file1
这是给下面的输出。
It is giving the following output.
HAI
LINE ONE LINE TWO
BYE
HAI
ONE
TWO
但我想输出是:
HAI
LINE ONE
LINE TWO
BYE
HAI
ONE
TWO
我怎样才能做到这一点?我试图保持\\ n,而是它给错误。
How can I achieve this? I tried to keep the \n but it is giving errors.
推荐答案
这个替换您的sed命令:
Replace your sed command with this:
sed $VAR' a\
LINE ONE\
LINE TWO
' file1
BTW你刚才的grep,awk中也可以减少到这一点:
btw your earlier grep, awk can also be reduced to this:
VAR=$(awk '$1 == "HAI" && NR==1{print NR}' file1)
高得多是得到这样的单awk命令完整的答案:
Much Better is to get full answer in single awk command like this:
awk '{if ($1=="HAI" && done!=1) {done=1; printf("%s\nLINE ONE\nLINE TWO\n", $0);} \
else print $0}' file1
OUTPUT:
HAI
LINE ONE
LINE TWO
BYE
HAI
ONE
TWO
这篇关于加入SED连续两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文