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 @ 2024-02-20 11:01  小鲨鱼2018  阅读(132)  评论(0编辑  收藏  举报