如何在模式之前和行号之后使用sed将一行插入到文件中? 以及如何使用相同的shell脚本?
这样在每个模式行之前插入一行:
sed '/Sysadmin/i Linux Scripting' filename.txt
这改变了这个使用行号范围:
sed '1,$ s/A/a/'
所以现在如何使用这两个(我不能)在一个模式之前和一个行号或另一种方法之后使用sed将一行插入到文件中?
我如何列出Nginx中的所有虚拟主机
系统调用如何被信号中断?
以root身份保存所有权移动文件linux
不带-g选项的编译调用堆栈(gcc编译器)
如何使用命令行工具将透明的OS X样式边框添加到图像中?
ashmem有什么特别的权力?
exec n <m与exec n>&m – 基于Sobell的Linux书
Python安装,无法findbz2模块
编译并build立“android”设备的“iw”?
sed -f sed.script file1 ...
或者你可以使用(多个) -e 'command'选项:
sed -e '/SysAdmin/i Linux Scripting' -e '1,$s/A/a/' file1 ...
如果你想在一行后附加一些东西,那么:
sed -e '234a Text to insert after line 234' file1 ...
我假设你只想在当前行号大于某个值的情况下在行之前插入行(即,如果行号在行号之前,则什么都不做)
如果你不被绑定到sed :
awk -v lineno=$line -v patt="$pattern" -v text="$line_to_insert" ' NR > lineno && $0 ~ patt {print text} {print} ' input > output
示例文件test.txt:
hello line 1 hello line 2 hello line 3
脚本:
sed -n 'H;${x;s/^n//;s/hello line 2/hello new linen&/;p;}' test.txt > test.txt.2
hello line 1 hello new line hello line 2 hello line 3
NB! 请注意,sed已经开始将换行符替换为无空格 – 这是必需的,否则结果文件在开始时会有一个空行
脚本找到包含“hello line 2”的行,然后在上面插入一个新行 – “hello new line”
sed命令的解释:
sed -n: suppress automatic printing of pattern space H;${x;s/test/next/;p} /<pattern>/ search for a <pattern> ${} do this 'block' of code H put the pattern match in the hold space s/ substitute test for next everywhere in the space x swap the hold with the pattern space p Print the current pattern hold space.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。