linux中sed命令利用n;p选项输出奇数行、偶数行、或指定规则行
001、输出偶数行
[root@pc1 test1]# ls a.txt [root@pc1 test1]# cat a.txt ## 测试文本 01 01 02 02 03 03 04 04 05 05 06 06 07 07 08 08 09 09 10 10 [root@pc1 test1]# sed -n 'n;p' a.txt ## 输出偶数行 02 02 04 04 06 06 08 08 10 10
002、输出奇数行
[root@pc1 test1]# ls a.txt [root@pc1 test1]# cat a.txt ## 测试文本 01 01 02 02 03 03 04 04 05 05 06 06 07 07 08 08 09 09 10 10 [root@pc1 test1]# sed -n 'p;n' a.txt ## 输出奇数行 01 01 03 03 05 05 07 07 09 09
003、
[root@pc1 test1]# ls a.txt [root@pc1 test1]# cat a.txt ## 测试文本 01 01 02 02 03 03 04 04 05 05 06 06 07 07 08 08 09 09 10 10 [root@pc1 test1]# sed -n 'n;n;n;p' a.txt ## 每四行输出一行 04 04 08 08
004、输出每四行的第一行
[root@pc1 test1]# ls a.txt [root@pc1 test1]# cat a.txt ## 测试文本 01 01 02 02 03 03 04 04 05 05 06 06 07 07 08 08 09 09 10 10 [root@pc1 test1]# sed -n "p;n;n;n" a.txt ## 输出每四行的第一行 01 01 05 05 09 09
005、 奇数行加倍
[root@pc1 test1]# ls a.txt [root@pc1 test1]# cat a.txt ## 测试文本 01 01 02 02 03 03 04 04 05 05 06 06 [root@pc1 test1]# sed 'p;n' a.txt ## 奇数行加倍 01 01 01 01 02 02 03 03 03 03 04 04 05 05 05 05 06 06
006、每四行的最后一行加倍
[root@pc1 test1]# ls a.txt [root@pc1 test1]# cat a.txt ## 测试文本 01 01 02 02 03 03 04 04 05 05 06 06 [root@pc1 test1]# sed 'n;n;n;p' a.txt ## 每四行的最后一行加倍 01 01 02 02 03 03 04 04 04 04 05 05 06 06
。
007、每三行的随后一行加倍两次
[root@pc1 test1]# ls a.txt [root@pc1 test1]# cat a.txt ## 测试文本 01 01 02 02 03 03 04 04 05 05 06 06 [root@pc1 test1]# sed 'n;n;p;p' a.txt ## 每三行的最后一行加倍两次 01 01 02 02 03 03 03 03 03 03 04 04 05 05 06 06 06 06 06 06
。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律