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
复制代码

 。

 

posted @   小鲨鱼2018  阅读(184)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示