sed高级使用

简介:

1、名reverse脚本用sed编辑器脚本反转数据流的文本行

                h 将模式空间复制到保持空间
                H 将模式空间附加到保持空间
                g 将保持空间复制到模式空间
                G 将保持空间附加到模式空间
                x  交换模式空间和保持空间的内容

                p  打印模式空间

 

                n  提取数据流的下一行

    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@logicserver tmp]# vim reverse.sh
#!/bin/bash
#
sed -n '{
1 !G
h
$p
}' $ 1
~       
[root@logicserver tmp]# cat data1
the quick brown fox jumps over the lazy dog1
a quick brown fox jumps over the lazy dog2
the quick brown fox jumps over the lazy dog3
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog6
 
 
[root@logicserver tmp]# sh reverse.sh data1
the quick brown fox jumps over the lazy dog6
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog3
a quick brown fox jumps over the lazy dog2
the quick brown fox jumps over the lazy dog1


2、加位行间距

1
2
3
4
5
6
7
8
[root@logicserver tmp]# sed  'G'  data1
the quick brown fox jumps over the lazy dog1
a quick brown fox jumps over the lazy dog2
  
the quick brown fox jumps over the lazy dog3
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog6

如果末尾不想这个空白行

1
2
3
4
5
6
7
8
[root@logicserver tmp]# sed  '$!G'  data1
the quick brown fox jumps over the lazy dog1
a quick brown fox jumps over the lazy dog2
  
the quick brown fox jumps over the lazy dog3
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog6


3、删除已有空白行

1
2
3
4
5
6
7
[root@logicserver tmp]# sed  '/^$/d'  data1
the quick brown fox jumps over the lazy dog1
a quick brown fox jumps over the lazy dog2
the quick brown fox jumps over the lazy dog3
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog6


4、给文件中的行编号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  [root@logicserver tmp]# sed  '='  data1
1
the quick brown fox jumps over the lazy dog1
2
a quick brown fox jumps over the lazy dog2
3
4
the quick brown fox jumps over the lazy dog3
5
the quick brown fox jumps over the lazy dog4
6
the quick brown fox jumps over the lazy dog5
7
the quick brown fox jumps over the lazy dog6

这样可观性不是很好

1
2
3
4
5
6
7
[root@logicserver tmp]# sed  '='  data1 | sed  'N;s/\n/ /'
1  the quick brown fox jumps over the lazy dog1
2  a quick brown fox jumps over the lazy dog2
3  the quick brown fox jumps over the lazy dog3
4  the quick brown fox jumps over the lazy dog4
5  the quick brown fox jumps over the lazy dog5
6  the quick brown fox jumps over the lazy dog6


5、删除空白行

1
2
3
4
5
6
7
8
  [root@logicserver tmp]# sed  '/^$/d'  data1
the quick brown fox jumps over the lazy dog1
a quick brown fox jumps over the lazy dog2
the quick brown fox jumps over the lazy dog3
the quick brown fox jumps over the lazy dog4
the quick brown fox jumps over the lazy dog5
the quick brown fox jumps over the lazy dog6
[root@logicserver tmp]#


6、单行next的命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@digitcube-test1 tmp]# cat data1
the quick green fox jumps over the lazy cat1 
the quick green fox jumps over the lazy cat2 
the quick green fox jumps over the lazy cat3 
the quick green fox jumps over the lazy cat4 
the quick green fox jumps over the lazy cat5 
the quick green fox jumps over the lazy cat6
 
[root@digitcube-test1 tmp]# sed  '/cat2/{n;d}'  data1
the quick green fox jumps over the lazy cat1 
the quick green fox jumps over the lazy cat2 
the quick green fox jumps over the lazy cat4 
the quick green fox jumps over the lazy cat5 
the quick green fox jumps over the lazy cat6   


7、合并文本行

 N的命令将下一行合并到那一行,然后用替换命令s将换行符转换成空格

1
2
3
4
5
6
[root@digitcube-test1 tmp]# sed  '/cat1/{N;s/\n/ /}'  data1
the quick green fox jumps over the lazy cat1  the quick green fox jumps over the lazy cat2 
the quick green fox jumps over the lazy cat3 
the quick green fox jumps over the lazy cat4 
the quick green fox jumps over the lazy cat5 
the quick green fox jumps over the lazy cat6   


8、删除结尾空白行

  data1末尾多两行空白行


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@digitcube-test1 tmp]# vim sed1.sh
 
#!/bin/bash
#
 
sed '{
:start
/^\n*$/{$d;N;b start}
}' $ 1
 
 
[root@digitcube-test1 tmp]# sh sed1.sh data1
the quick green fox jumps over the lazy cat1 
the quick green fox jumps over the lazy cat2 
the quick green fox jumps over the lazy cat3 
the quick green fox jumps over the lazy cat4 
the quick green fox jumps over the lazy cat5 
the quick green fox jumps over the lazy cat6       

 

9、模式替代

and符号(&)用来代表替换命令中匹配的模式

1
2
[root@logicserver tmp]# echo  "The cat sleeps in his hat." |sed  's/.at/"&"/g'
The  "cat"  sleeps  in  his  "hat" .

当模式匹配了了单词cat,“cat”就出现在替换后的单词里,当它匹配了hat,“hat”就出现在替换后单词里

 

替换单独的单词

sed编辑器用圆括号来定义替换模式的子字符串,替代字符由反斜线和数字组成,数字表明字符串模块的位置,sed的编辑器给第一个模块分配字符
\1,给第二个模块分配字符\2,依此类推

1
2
3
[root@logicserver tmp]# echo  "my lover is pangfeng" |sed '
> s/my \(lover\)/qingyun \ 1 /'
qingyun lover  is  pangfeng


10、在两个或多个字符串模式中插入文本

1
2
3
4
5
6
[root@node3 conf]# echo  "hi tom what are you doing" |sed '{
> :start
> s/hi \(tom\)/hello \ 1  miki/
> t start
> }'
hello tom miki what are you doing
1
2
3
4
5
6
[root@node3 conf]# echo  "121234" |sed '{
:start                     
s/\(.*[ 0 - 9 ]\)\([ 0 - 9 ]\{ 3 \}\)/\ 1 .\ 2 /          
t start
}'
121.234










本文转自 zouqingyun 51CTO博客,原文链接:http://blog.51cto.com/zouqingyun/1695822,如需转载请自行联系原作者
目录
相关文章
|
机器学习/深度学习 算法 数据建模
探索XGBoost:时间序列数据建模
探索XGBoost:时间序列数据建模
414 2
Python之pandas:数据类型变换之object、category、bool、int32、int64、float64以及数据类型标准化之详细攻略
Python之pandas:数据类型变换之object、category、bool、int32、int64、float64以及数据类型标准化之详细攻略
Python之pandas:数据类型变换之object、category、bool、int32、int64、float64以及数据类型标准化之详细攻略
|
10月前
|
人工智能 IDE 安全
灵码编码搭子新功能体验
作为一名信息安全工程师,我对AI应用充满兴趣。通过“AI-Shifu”引导,在PyCharm中使用通义灵码完成了加密解密小项目。通义灵码与IDE深度融合,不仅提供代码解释、生成单元测试、优化代码等功能,还显著降低了初版代码的报错率,大大提升了开发效率。
|
7月前
|
JSON JavaScript 前端开发
处理从API返回的JSON数据时返回Unicode编码字符串怎么处理
在处理API返回的JSON数据时,遇到类似`\u7f51\u7edc\u8fde\u63a5\u9519\u8bef`的Unicode编码字符串,可使用JavaScript内置方法转换为可读文字。主要方法包括:1. 使用`JSON.parse`自动解析;2. 使用`decodeURIComponent`和`escape`组合解码;3. 在API调用中直接处理响应数据。这些方法能有效处理多语言内容,确保正确显示非ASCII字符。
|
存储 负载均衡 开发者
深入理解微服务架构中的服务发现机制
【7月更文挑战第19天】在微服务架构的海洋中,服务发现是一艘至关重要的航船,它指引着各个微服务之间的通信与协作。本文将揭开服务发现的神秘面纱,探索其工作原理、实现方式及面临的挑战,为开发者提供清晰的导航,确保服务间的顺畅航行。
|
9月前
|
人工智能 C语言
一则有意思的AI错误
本文记录了豆包AI在回答关于C语言内存分配问题时的错误过程。首次询问时,AI给出了错误的回答;经过两次追问和纠正后,AI才给出正确的答案。文中附有提问过程的完整截图和相关代码,分析了问题的原因,并探讨了AI在处理这类问题时的局限性。
242 0
|
人工智能 搜索推荐 算法
智能之网:AI在互联网技术中的应用与挑战
本文深入探讨了人工智能(AI)如何革新互联网技术,提升用户体验,并优化数据处理流程。通过分析AI在搜索引擎、社交媒体、网络安全以及个性化服务中的实际应用,揭示了AI技术带来的便捷与效率。同时,文章也未忽视伴随技术进步而来的隐私保护、算法偏见和人机关系等挑战,为读者提供了一个全面而深刻的视角。
|
机器学习/深度学习 存储 算法
基于YOLOv8深度学习的血细胞检测与计数系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战、智慧医疗
基于YOLOv8深度学习的血细胞检测与计数系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战、智慧医疗
|
PyTorch 算法框架/工具
时间序列预测:CNN+LSTM+Attention模型实战
时间序列预测:CNN+LSTM+Attention模型实战
1695 0