如何在模式之前和行号之后使用 sed 插入一行? [英] How to insert a line using sed before a pattern and after a line number?

查看:20
本文介绍了如何在模式之前和行号之后使用 sed 插入一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 sed 在模式之前和行号之后将一行插入到文件中?以及如何在shell脚本中使用相同的内容?

How to insert a line into a file using sed before a pattern and after a line number? And how to use the same in shell script?

这会在每行之前插入一行模式:

This inserts a line before every line with the pattern :

sed '/Sysadmin/i  Linux Scripting' filename.txt

这使用行号范围改变了这一点:

And this changes this using line number range :

sed '1,$ s/A/a/'

那么现在如何使用这两者(我不能)在模式之前和行号或其他方法之后使用 sed 将一行插入到文件中?

So now how to use these both (which I couldn't) to insert a line into a file using sed before a pattern and after a line number or another approach?

推荐答案

您可以编写一个 sed 脚本文件并使用:

You can either write a sed script file and use:

sed -f sed.script file1 ...

或者你可以使用(多个)-e 'command' 选项:

Or you can use (multiple) -e 'command' options:

sed -e '/SysAdmin/i
Linux Scripting' -e '1,$s/A/a/' file1 ...

如果你想在一行后附加一些东西,那么:

If you want to append something after a line, then:

sed -e '234a
Text to insert after line 234' file1 ...

这篇关于如何在模式之前和行号之后使用 sed 插入一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆