awk之提取含有相同段行的第一行和最后一行

转自http://blog.chinaunix.net/uid-10540984-id-1761379.html

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

 

可能看着有点眼花,就是说如何提取每天的第一行数据或最后一行数据,红色就是第一行,蓝色就是最后一行。

 

 

数组解法:

  1. # 只提取每天的第一行数据
  2. awk '!a[$1]++' file
  3. awk '++a[$1]==1' file
  4.  
  5. # 只提取每天的最后一行数据
  6. awk '{a[$1]=$0}END{for(i=1;i<=asort(a);i++)print a[i]}' file
  7. awk '!a[$1]++&&i{print i}{i=$0}END{print i}' file

 

非数组解法:

  1. # 只提取每天第一行数据
  2. awk '$1!=x{x=$1;print}' file
  3.  
  4. # 只提取每天最后一行数据
  5. awk 'NR>1{if($1!=x)print y}{x=$1;y=$0}' file <(echo)

话说数组的效率那确实在大文件下够慢的,别看非数组的命令比较长点,效率那可是高的。

 

sed解法:

  1. # 只提取每天第一行数据
  2. sed -r ':a;$!N;s/([^ ]+)( +[^\n]+)\n\1.*/\1\2/;ta;P;D' file
  3.  
  4. # 只提取每天最后一行数据
  5. sed -r ':a;$!N;s/([^ ]+) +[^\n]+\n\1(.*)/\1\2/;ta;P;D' file
  6. sed -r '$!N;/([^ ]+ ).*\n\1/!P;D' file

猜你喜欢

转载自blog.csdn.net/wqfhenanxc/article/details/81117498