Sed常用功能个人整理

Sed常用功能个人整理

2019.06.24 10:23:41字数 240阅读 15

Sed对1G以下的数据效率很高这里介绍一些个人在工作中遇到的sed问题

  • 1.查找字段

以文章test.txt为例:

ID        type         old  new
A01_1   1_34189552   0|0  0|0
A01_2   1_65117762   1|0  1|0
A01_3   1_70543349   0|0  0|0
A01_4   1_77765794   0|1  0|1
A01_5   1_109674087  0|1  0|1
A01_6   1_194530914  0|0  0|0
A01_7   1_224812701  0|0  0|0
A01_8   2_3645429    1|0  1|0
A01_9   2_21527764   0|0  0|0
A01_10  2_28792335   0|0  0|0
A01_11  2_142074734  0|0  0|0

单个查找,查找字段以p结尾,例如:

sed -n '/77765794/p' test.txt
>结果:
A01_9672  1_77765794   0|1  0|1
* 若不加n则会输出 查询结果+test.txt文本内容(与-e输出相同):
ID        type         old  new
A01_1   1_34189552   0|0  0|0
A01_2   1_65117762   1|0  1|0
A01_3   1_70543349   0|0  0|0
A01_4   1_77765794   0|1  0|1
A01_4   1_77765794   0|1  0|1#会输出两次
A01_5   1_109674087  0|1  0|1
A01_6   1_194530914  0|0  0|0
A01_7   1_224812701  0|0  0|0
A01_8   2_3645429    1|0  1|0
A01_9   2_21527764   0|0  0|0
A01_10  2_28792335   0|0  0|0
A01_11  2_142074734  0|0  0|0

指定行输出
*sed -n 'star,end p' file 包含star和end行,$表示尾行

sed -n '1p' test.txt
ID        type         old  new

sed -n '1,2p' test.txt
ID        type         old  new
A01_1   1_34189552   0|0  0|0

sed -n '1p;3p' test.txt    # 输出多行
sed -n '{1p;3p}' test.txt  #  用分号来隔离多个操作(如果有定址条件,则应该使用{ }括起来)
ID        type         old  new
A01_2   1_65117762   1|0  1|0

多个查找,以 ‘|’ 分割关键字

sed -n '/34189552|70543349/p' test.txt
结果如下:
A01_1   1_34189552   0|0  0|0
A01_3   1_70543349   0|0  0|0
*这里必须加''引号,不然不返回任何值

前后替换

sed -e 's/1_109674087/_&_/' test.txt # 用&替代整个查找字符串
ID        type         old  new
A01_1   1_34189552   0|0  0|0
A01_2   1_65117762   1|0  1|0
A01_3   1_70543349   0|0  0|0
A01_4   1_77765794   0|1  0|1
A01_5   _1_109674087_  0|1  0|1
A01_6   1_194530914  0|0  0|0
A01_7   1_224812701  0|0  0|0
A01_8   2_3645429    1|0  1|0
A01_9   2_21527764   0|0  0|0
A01_10  2_28792335   0|0  0|0
A01_11  2_142074734  0|0  0|0

*注意Linux下shell的正则表达式与python支持的字符有所不同

正则表达式支持情况

  • 2.替换字段

s/被替换内容/替换内容/

sed -e 's/1_65117762/2_222/' test.txt
*这里使用-n不输出,需要使用-e
ID        type         old  new
A01_1   1_34189552   0|0  0|0
A01_2   2_222        1|0  1|0   # 不会出现两次,直接显示替换的内容
A01_3   1_70543349   0|0  0|0
A01_4   1_77765794   0|1  0|1
A01_5   1_109674087  0|1  0|1
A01_6   1_194530914  0|0  0|0
A01_7   1_224812701  0|0  0|0
A01_8   2_3645429    1|0  1|0
A01_9   2_21527764   0|0  0|0
A01_10  2_28792335   0|0  0|0
A01_11  2_142074734  0|0  0|0

替换两个字符中间的内容
*这里需要使用 -r:启用扩展的正则表达式,若与其他选项一起使用,应作为首个选项

sed -r  's/(A01_11).*(0|0)/1 2_222 2/g' test.txt
*这里的1 代指第一个括号的内容 2代指第二个括号的内容
结果如下:
ID        type         old  new
A01_1   1_34189552   0|0  0|0
A01_2   1_65117762   1|0  1|0
A01_3   1_70543349   0|0  0|0
A01_4   1_77765794   0|1  0|1
A01_5   1_109674087  0|1  0|1
A01_6   1_194530914  0|0  0|0
A01_7   1_224812701  0|0  0|0
A01_8   2_3645429    1|0  1|0
A01_9   2_21527764   0|0  0|0
A01_10  2_28792335   0|0  0|0
A01_11 2_222 0 # 将最后一个中间字符改为' 2_222 '

指定需要替换的行和第几个

sed -e '2s/0/o/3' test.txt
将第2行第3个0替换成o
ID        type         old  new
A01_1   1_34189552   0|o  0|0 # 0被替换成了o
A01_2   1_65117762   1|0  1|0
A01_3   1_70543349   0|0  0|0
A01_4   1_77765794   0|1  0|1
A01_5   1_109674087  0|1  0|1
A01_6   1_194530914  0|0  0|0
A01_7   1_224812701  0|0  0|0
A01_8   2_3645429    1|0  1|0
A01_9   2_21527764   0|0  0|0
A01_10  2_28792335   0|0  0|0
A01_11  2_142074734  0|0  0|0
  • 3.删除字段

删除字段需要使用关键字d,sed -e /内容/d file

sed -e ' /1_34189552/d' test.txt
结果如下:
ID        type         old  new
A01_2   1_65117762   1|0  1|0
A01_3   1_70543349   0|0  0|0
A01_4   1_77765794   0|1  0|1
A01_5   1_109674087  0|1  0|1
A01_6   1_194530914  0|0  0|0
A01_7   1_224812701  0|0  0|0
A01_8   2_3645429    1|0  1|0
A01_9   2_21527764   0|0  0|0
A01_10  2_28792335   0|0  0|0
A01_11  2_142074734  0|0  0|0

指定行删除

sed -e '$d' test.txt
*注意有些表达式可以不加''引号,带有正则的表达式必须加引号
ID        type         old  new
A01_1   1_34189552   0|0  0|0
A01_2   1_65117762   1|0  1|0
A01_3   1_70543349   0|0  0|0
A01_4   1_77765794   0|1  0|1
A01_5   1_109674087  0|1  0|1
A01_6   1_194530914  0|0  0|0
A01_7   1_224812701  0|0  0|0
A01_8   2_3645429    1|0  1|0
A01_9   2_21527764   0|0  0|0
A01_10  2_28792335   0|0  0|0

其他操作

删除所有空行
sed  '/^$/d' a.txt
删除多行
sed '/2_28792335/d;/2_3645429/d' a.txt
 
 
0人点赞
 
原文地址:https://www.cnblogs.com/xuanbjut/p/12783531.html