Linux 使用sed在模式后第n行的文件中插入字符串
我想使用sed在模式的第n行之后插入单词。 我想修改这个命令,但它只在模式后的第一行插入Linux 使用sed在模式后第n行的文件中插入字符串,linux,sed,insert,match,line,Linux,Sed,Insert,Match,Line,我想使用sed在模式的第n行之后插入单词。 我想修改这个命令,但它只在模式后的第一行插入 sed -i '/myPattern/a \ LineIWantToinser ' myFile 我应该使用什么命令来插入例如在模式后的第三行中 sed '/pattern/{N;N;N;i \ Line to add after 3 lines with patterne as starting counter ' YourFile 用于在图案和插入线之间添加线的编号N 3行中没有检查文件结尾或模式
sed -i '/myPattern/a \ LineIWantToinser ' myFile
我应该使用什么命令来插入例如在模式后的第三行中
sed '/pattern/{N;N;N;i \
Line to add after 3 lines with patterne as starting counter
' YourFile
- 用于在图案和插入线之间添加线的编号
N
- 3行中没有检查文件结尾或模式。(采购订单中未指定)
- 使用GNU sed实现这一点的最简单方法是。。(可能存在一些直接的解决方案!)
查看模式所在的行(grep也可以在此处与-n一起使用)
然后,如果linenumber+numoflines是例如123
sed '123aSOME INSERTED TEXT AFTER THAT LINE' file
其中小a是append命令(在该行之后,如果使用i,将是预模式行)
另外,我很想看看@neronlevelu(或其他sed爱好者)是否会找到更好的sed解决方案
编辑:我找到了,它似乎是一个附加或插入我必须?当使用{with;inside}like时,位于第一行位置
sed '/pattern/{N;N;N;
a SOME TEXT FOR INSERTING
}' file
带有
bash
和ed
的版本:
ed -s myFile <<<$'/myPattern/+3a\n LineIWantToinser \n.\nwq'
ed-s myFile您想要在具有该模式的行之后的第n行吗?还是第n行之后的那一行?或者是第n行之后的第n行?无论您想要哪种情况sed
都可能不是最好的工具awk
可能更容易(尽管您应该能够很容易地使用sed
完成其中一些操作)。
ed -s myFile <<<$'/myPattern/+3a\n LineIWantToinser \n.\nwq'