使用awk和sed获取文件奇偶数行的方法总结

使用awk和sed获取文件奇偶数行的方法总结

测试文件test.file

[root@localhost ~]# cat test.file

111111111111111

222222222222222

333333333333333

444444444444444

555555555555555

666666666666666

777777777777777

888888888888888

999999999999999

1010101010101010

1) 打印奇数行的方法

[root@localhost ~]# sed -n '1~2p' test.file

111111111111111

333333333333333

555555555555555

777777777777777

999999999999999

[root@localhost ~]# sed -n 'p;n' test.file

111111111111111

333333333333333

555555555555555

777777777777777

999999999999999

[root@localhost ~]# sed -n '1,$p;n' test.file

111111111111111

333333333333333

555555555555555

777777777777777

999999999999999

[root@localhost ~]# sed '2~2d' test.file

111111111111111

333333333333333

555555555555555

777777777777777

999999999999999

[root@localhost ~]# awk 'NR%2==1' test.file

111111111111111

333333333333333

555555555555555

777777777777777

999999999999999

[root@localhost ~]# awk 'NR%2' test.file

111111111111111

333333333333333

555555555555555

777777777777777

999999999999999

[root@localhost ~]# awk 'i=!i' test.file

111111111111111

333333333333333

555555555555555

777777777777777

999999999999999

2) 打印偶数行的方法

[root@localhost ~]# sed -n '2~2p' test.file

222222222222222

444444444444444

666666666666666

888888888888888

1010101010101010

[root@localhost ~]# sed -n 'n;p' test.file

222222222222222

444444444444444

666666666666666

888888888888888

1010101010101010

[root@localhost ~]# sed -n '1,$n;p' test.file

222222222222222

444444444444444

666666666666666

888888888888888

1010101010101010

[root@localhost ~]# sed '1~2d' test.file

222222222222222

444444444444444

666666666666666

888888888888888

1010101010101010

[root@localhost ~]# awk 'NR%2==0' test.file

222222222222222

444444444444444

666666666666666

888888888888888

1010101010101010

[root@localhost ~]# awk '!(NR%2)' test.file

222222222222222

444444444444444

666666666666666

888888888888888

1010101010101010

[root@localhost ~]# awk '!(i=!i)' test.file

222222222222222

444444444444444

666666666666666

888888888888888

1010101010101010

打印奇偶行的方法总结

sed -n '1~2p' test.file 打印奇数行

sed -n '2~2p' test.file 打印偶数行

sed -n 'p;n' test.file 打印奇数行

sed -n 'n;p' test.file 打印偶数行

sed -n '1,$p;n' test.file 打印奇数行

sed -n '1,$n;p' test.file 打印偶数行

sed '2~2d' test.file 打印奇数行

sed '1~2d' test.file 打印偶数行

awk 'NR%2==1' test.file 打印奇数行

awk 'NR%2==0' test.file 打印偶数行

awk 'NR%2' test.file 打印奇数行

awk '!(NR%2)' test.file 打印偶数行

awk 'i=!i' test.file 打印奇数行

awk '!(i=!i)' test.file 打印偶数行

其他相关正则取值说明

1) 打印行号和内容

[root@localhost ~]# awk '{print NR":"$0}' test.file

1:111111111111111

2:222222222222222

3:333333333333333

4:444444444444444

5:555555555555555

6:666666666666666

7:777777777777777

8:888888888888888

9:999999999999999

10:1010101010101010

2) 每行间加一个空行

[root@localhost ~]# awk '1; { print "" }' test.file

111111111111111

222222222222222

333333333333333

444444444444444

555555555555555

666666666666666

777777777777777

888888888888888

999999999999999

1010101010101010

[root@localhost ~]# awk '1 { print } { print "" }' test.file

111111111111111

222222222222222

333333333333333

444444444444444

555555555555555

666666666666666

777777777777777

888888888888888

999999999999999

1010101010101010

[root@localhost ~]# awk '{ print } { print "" }' test.file

111111111111111

222222222222222

333333333333333

444444444444444

555555555555555

666666666666666

777777777777777

888888888888888

999999999999999

1010101010101010

[root@localhost ~]# awk 'BEGIN { ORS="\n\n" }; 1' test.file

111111111111111

222222222222222

333333333333333

444444444444444

555555555555555

666666666666666

777777777777777

888888888888888

999999999999999

1010101010101010

3) 仅输出非空行,并每行间在加一个空行

# awk 'NF { print $0 "\n" }' test.file

NF表示当前行的字段数,$0表示当前行,最后再加一个换行

4) 双倍行距;没行间两个空行

默认输出后会换行的,输出\n,则会输出两个空白行

[root@localhost ~]# awk '1; { print "\n" }' test.file

111111111111111

222222222222222

333333333333333

444444444444444

555555555555555

666666666666666

777777777777777

888888888888888

999999999999999

1010101010101010

[root@localhost ~]# awk '{ print; print "\n" }' test.file

111111111111111

222222222222222

333333333333333

444444444444444

555555555555555

666666666666666

777777777777777

888888888888888

999999999999999

1010101010101010

5) 显示当前行在所在文件中的行号

FNR,表示当前行在文件中的行号

[root@localhost ~]# awk '{ print FNR "\t" $0 }' test.file

1 111111111111111

2 222222222222222

3 333333333333333

4 444444444444444

5 555555555555555

6 666666666666666

7 777777777777777

8 888888888888888

9 999999999999999

10 1010101010101010

6) 显示当前行在本次处理过程中的行号

NR,表示当前行在本次处理过程中的行号

[root@localhost ~]# awk '{ print NR "\t" $0 }' test.file

1 111111111111111

2 222222222222222

3 333333333333333

4 444444444444444

5 555555555555555

6 666666666666666

7 777777777777777

8 888888888888888

9 999999999999999

10 1010101010101010

为啥有FNR和NR的差别呢?效果不都是一样么?

如果使用两个文件filname1.ext filname2.ext,则就会看到差别了。

原来:FNR,是每个文件中的,换了一个文件,会归零;而NR则每个文件会累加起来的

7) 使用简单样式来输出

下面表示"行号占用5位,不足补空格"

[root@localhost ~]# awk '{ printf("] : %s\n", NR, $0) }' test.file

] : 1

] : 2

] : 3

] : 4

] : 5

] : 6

] : 7

] : 8

] : 9

] : 10

8) 显示非空行

# awk 'NF { $0=++a " :" $0 }; { print }' test.file

NF前面说了,表示当前行的行号,此处用他作为条件,如果是空行,则NF为0,跳过;否则,用动态变量a存储非空行的数目

9) 计算行数:效果类似wc -l

END表示每行都处理完了后,在执行,此时NR就是最后一行的行号,也就是总的行数了。

#awk 'END { print NR }' test.file

10) 计算每一行的和

s用作每行和的累加,从1到NF(每行总的字段数),依次累加

# awk '{ s = 0; for (i = 1; i <= NF; i++) s = s+$i; print s }' test.file

11) 计算文件中所有字段的和

s用作总和的累加,每行都处理完成了,再输出s;注意和10对比,此处没有每行清零,所以累加了。没有设置的变量,默认为空,

但是会根据上下文数值计算情况自动变为0

# awk '{ for (i = 1; i <= NF; i++) s = s+$i }; END { print s }' test.file

12) 将每个字段用其绝对值代替

$i表示当前行中的字段,$0表示当前行,可以改变$i的值

# awk '{ for (i = 1; i <= NF; i++) if ($i < 0) $i = -$i; print }' test.file

13) 计算文件中总的字段和(例如计算单词数)

# awk '{ total = total + NF }; END { print total }' test.file

14) 计算匹配指定信息的总行数

# awk '/Linux/ { n++ }; END { print n+0 }' test.file

15) 找到文件中每行第一个字段中,最大的数,以及其所在的行

用max存储最大的数,maxline存储最大数所在的行,并在最后输出

# awk '$1 > max { max=$1; maxline=$0 }; END { print max, maxline }' test.file

16) 显示当前行的字段数,并输出当前行

# awk '{ print NF ":" $0 } ' test.file

17) 显示每行最后一个字段的内容

# awk '{ print $NF }' test.file #NF表示当前行的字段数,例如为3,则$NF,就是$3,也就是第三个字段了

18) 显示最后一行的最后一个字段

每行处理没有输出,尽在最后输出,field作为每行的最后一行的暂存变量

# awk '{ field = $NF }; END { print field }' test.file

19) 显示字段数小于4的行

# awk 'NF < 4' test.file

20) 显示每行的最后一个字段小于4的行

# awk '$NF < 4' test.file

shell脚本,实现奇数行等于偶数行

[root@localhost ~]# cat kevin.file

aa

11

bb

22

cc

33

dd

44

使用awk命令可以这样实现:

[root@localhost ~]# awk 'NR%2==0{print a"="$0}{a=$0}' kevin.file

aa=11

bb=22

cc=33

dd=44

使用shell脚本可以这样实现:

[root@localhost ~]# cat kevin.sh

#!/bin/bash

n=0

for i in $(cat /root/kevin.file)

do

n=$(($n+1))

[ $((n%2)) -eq 1 ] && echo -n $i=

[ $((n%2)) -eq 0 ] && echo $i

done

[root@localhost ~]# sh kevin.sh

aa=11

bb=22

cc=33

dd=44

相关推荐

原神能量对冲的正确方法
365bet提款要求

原神能量对冲的正确方法

📅 11-26 👁️ 4940
哪里的声音最好听
bat365bet

哪里的声音最好听

📅 01-09 👁️ 3481
DNF出血机制分析 告诉你太白出血加成原理
365bet官方网站是多少

DNF出血机制分析 告诉你太白出血加成原理

📅 07-16 👁️ 9451