awk之按特征相邻行去重(保留第一行或最后一行)
http://blog.chinaunix.net/uid-10540984-id-1761379.html
file:尾巴内有一个#
代表该天第一个数据 三个#
代表最后一个数据
2011-07-06 0:38:40 copy file 192.168.1.67 speed: 2.256 MBps #
2011-07-06 1:30:46 copy file 192.168.1.67 speed: 2.000 MBps
2011-07-06 2:30:43 copy file 192.168.1.67 speed: 2.095 MBps ###
2011-07-07 0:33:43 copy file 192.168.1.67 speed: 2.146 MBps #
2011-07-07 1:30:43 copy file 192.168.1.67 speed: 2.095 MBps
2011-07-07 2:30:43 copy file 192.168.1.67 speed: 2.146 MBps
2011-07-07 2:32:55 copy file 192.168.1.67 speed: 5.126 MBps ###
2011-07-08 0:31:01 copy file 192.168.1.67 speed: 1.508 MBps #
2011-07-08 1:32:51 copy file 192.168.1.67 speed: 1.816 MBps ###
2011-07-09 0:32:44 copy file 192.168.1.67 speed: 2.119 MBps #
2011-07-09 1:34:48 copy file 192.168.1.67 speed: 1.935 MBps
2011-07-09 2:30:57 copy file 192.168.1.67 speed: 1.618 MBps
2011-07-09 6:05:57 copy file 192.168.1.67 speed: 1.358 MBps
2011-07-09 8:30:57 copy file 192.168.1.67 speed: 1.618 MBps ###
2011-07-10 0:30:55 copy file 192.168.1.67 speed: 1.679 MBps #
2011-07-10 1:31:00 copy file 192.168.1.67 speed: 1.534 MBps ###
如何提取每天的第一行数据或最后一行数据,尾巴内有一个#
,尾巴内有三个#
。
数组解法:
# 只提取每天的第一行数据
awk '!a[$1]++' file
awk '++a[$1]==1' file
# 只提取每天的最后一行数据
awk '{a[$1]=$0}END{for(i=1;i<=asort(a);i++)print a[i]}' file
awk '!a[$1]++&&i{print i}{i=$0}END{print i}' file
非数组解法:
# 只提取每天第一行数据
awk '$1!=x{x=$1;print}' file
# 只提取每天最后一行数据
awk 'NR>1{if($1!=x)print y}{x=$1;y=$0}' file <(echo)
话说数组的效率那确实在大文件下够慢的,别看非数组的命令比较长点,效率那可是高的。
sed解法:
# 只提取每天第一行数据
sed -r ':a;$!N;s/([^ ]+)( +[^\n]+)\n\1.*/\1\2/;ta;P;D' file
# 只提取每天最后一行数据
sed -r ':a;$!N;s/([^ ]+) +[^\n]+\n\1(.*)/\1\2/;ta;P;D' file
sed -r '$!N;/([^ ]+ ).*\n\1/!P;D' file
perl 批量替换test.properties 中的AAD为 BBC
> perl -p -i -e 's/AAD/BBC/g' test.properties
perl 删除匹配到的行,后面的N行
$ cat delete_5lines.txt
1
2
3
4
5 hello
6
7
8
9
10
$ perl -ne ' BEGIN{$y=1} $y=$. if /hello/ ; print if $y==1 or $.-$y > 5 ' delete_5lines.txt
1
2
3
4
11 hai
网友评论