添加文件内容:
加头 sed 's/^/new_content /' tmp
加尾 sed 's/$/new_content /' tmp
复制第行内容:sed 's/\(^.*$\)/\1 \1/' yourfile
配置多个关键字:
sed -n '/abc\|efg/p' test.txt |
1 |
#行前加 |
2 | sed -i '/abc/i\new line1' test.txt |
3 |
#行前后 |
4 |
sed -i '/abc/a\new line2' test.txt |
测试文件内容如下:
匹配模式通常被包含在一对斜扛之中,下面是几种常用模式:cat temp.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.
格式一:PATTERN
仅输出匹配 PATTERN 的一行。
> sed -n /Hardware/p temp.txt
3. Hardware
格式二:/PATTERN/,ADDRESS
输出匹配 PATTERN 的那行以及到达 ADDRESS 间的所有行。
sed -n '/Hardware/,6p' temp.txt
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
格式三:ADDRESS,/PATTERN/
输出从地址 ADDRESS 开始的行,直到匹配 PATTERN 那行。
sed -n '3,/Security/p' temp.txt
3. Hardware
4. Security (Firewall, Network, Online Security etc)
格式四:/PATTERN,$
输出从匹配 PATTERN 的行直到末行中的所有行。
sed -n '/Security/,$p' temp.txt
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.
格式五:/PATTERN/,+N
输出匹配 PATTERN 的行以及跟随在其后的 N 行:
sed -n '/Security/,+3p' temp.txt
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
格式六:/PATTERN/,/PATTERN/
输出符合匹配两个 PATTERN 之间的所有行(包括匹配行),实际应用中,这里的 PATTERN 往往都会用正则表达式。
sed -n '/Linux/,/websites/p' temp.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
使用sed在文本中定位文本的方式:
x x为一行号,比如1
x,y 表示行号范围从x到y,如2,5表示从第2行到第5行
/pattern/ 查询包含模式的行,如/disk/或/[a-z]/
/pattern/pattern/ 查询包含两个模式的行,如/disk/disks/
/pattern/,x 在给定行号上查询包含模式的行,如/disk/,3
x,/pattern/ 通过行号和模式查询匹配行,如 3,/disk/
x,y! 查询不包含指定行号x和y的行
基本sed编辑命令:
p 打印匹配行 c/ 用新文本替换定位文本
= 显示文件行号 s 使用替换模式替换相应模式
a/ 在定位行号后附加新文本信息 r 从另一个文本中读文本
i/ 在定位行号后插入新文本信息 w 写文本到一个文件
d 删除定位行 q 第一个模式匹配完成后退出或立即退出
l 显示与八进制ASCII代码等价的控制字符 y 传送字符
n 从另一个文本中读文本下一行,并附加在下一行 {} 在定位行执行的命令组
g 将模式2粘贴到/pattern n/
基本sed编程举例:
使用p(rint)显示行: sed -n '2p' temp.txt 只显示第2行,使用选项n
打印范围: sed -n '1,3p' temp.txt 打印第1行到第3行
打印模式: sed -n '/movie/'p temp.txt 打印含movie的行
使 用模式和行号查询: sed -n '3,/movie/'p temp.txt 只在第3行查找movie并打印
显示整个文件: sed -n '1,$'p temp.txt $为最后一行
任意字符: sed -n '/.*ing/'p temp.txt 注意是.*ing,而不是*ing
打印行号: sed -e '/music/=' temp.txt
附加文本:(创建sed脚本文件)chmod u+x script.sed,运行时./script.sed temp.txt
#!/bin/sed -f
/name1/ a/ #a/表示此处换行添加文本
HERE ADD NEW LINE. #添加的文本内容
插入文本: /name1/ a/ 改成 4 i/ 4表示行号,i插入
修改文本: /name1/ a/ 改 成 /name1/ c/ 将修改整行,c修改
删除文本: sed '1d' temp.txt 或者 sed '1,4d' temp.txt
替 换文本: sed 's/source/OKSTR/' temp.txt 将source替换成OKSTR
sed 's//$//g' temp.txt 将文本中所有的$符号全部删除
sed 's/source/OKSTR/w temp2.txt' temp.txt 将替换后的记录写入文件temp2.txt
替换修改字符串: sed 's/source/"ADD BEFORE" &/p' temp.txt
结果将在source字符串前面加上"ADD BEFORE",这里的&表示找到的source字符并保存
sed结果写入到文件: sed '1,2 w temp2.txt' temp.txt
sed '/name/ w temp2.txt' temp.txt
从文件中读文本: sed '/name/r temp2.txt' temp.txt
在每列最后加文本: sed 's/[0-9]*/& Pass/g' temp.txt
从 shell向sed传值: echo $NAME | sed "s/go/$REP/g" 注意需要使用双引号
快速一行命令:
's//.$//g' 删除以句点结尾行
'-e /abcd/d' 删除包含abcd的行
's/[][][]*/[]/g' 删除一个以上空格,用一个空格代替
's/^[][]*//g' 删除行首空格
's//.[][]*/[]/g' 删除句号后跟两个或更多的空格,用一个空格代替
'/^$/d' 删除空行
's/^.//g' 删除第一个字符,区别 's//.//g'删除所有的句点
's/COL/(.../)//g' 删除紧跟COL的后三个字母
's/^////g' 删除路径中第一个/
sed '1{:a;N;6!ba};N;${P;Q};D'
awk '{a[NR%7]=$0}END{print a[(NR-6)%7]}'
从性能上来讲,倒数几行的问题,还是用tail的好
tail -7 | head -1
sed -n 11,$ !p Print first 10 lines
sed 1,10 !d Print first 10 lines
sed 11,$ d Print first 10 lines
--------------------------------------------------------
sed -n 1,10 !p Print last 10 lines
sed -n 11,$ p Print last 10 lines
sed 1,10 d Print last 10 lines
sed 11,$ !d Print last 10 lines
--------------------------------------------------------
sed -n 1,10 d Nothing printed
sed -n 1,10 !d Nothing printed
sed -n 11,$ d Nothing printed
sed -n 11,$ !d Nothing printed
--------------------------------------------------------
sed 1,10 p Print first 10 lines twice,
Then next 10 lines once
sed 11,$ !p Print first 10 lines twice,
Then last 10 lines once
--------------------------------------------------------
sed 1,10 !p Print first 10 lines once,
Then last 10 lines twice
sed 11,$ p Print first 10 lines once,
then last 10 lines twice
This table shows that the following commands are identical:
sed -n '1,10 p'sed -n '11,$ !p'
sed '1,10 !d'
sed '11,$ d'