awk之提取含有相同段行的第一行和最后一行
2013-08-26 14:54
218 查看
原文地址:awk之提取含有相同段行的第一行和最后一行 作者:zooyo
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
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
相关文章推荐
- 批处理提取文本文件第一行跟最后一行的方法
- linux编辑文本(vim)时跳转到最后一行和第一行及相关指令
- 向DataGridView中添加新的一行数据,可以添加到最后一行或作为第一行
- vi中跳到文件的第一行和最后一行
- 每天一道LeetCode-----杨辉三角从第一行到最后一行的最小路径和
- Vim快速移动光标至行首和行尾 、第一行和最后一行
- mysql 查询第几行到第几行记录 查询最后一行和第一行记录 查询前几行和后几行记录
- vi中跳到文件的第一行和最后一行
- vi 调到第一行,或最后一行
- Jquery删除表格除第一行和最后一行之外的所有行
- DataTable获取第一行和最后一行的数据
- GridView和ListView设置第一行和最后一行的边距
- js清理掉table中的除了第一行和最后一行
- sed或awk处理文件最后一行的实现方法
- vi中跳到文件的第一行和最后一行
- 用DTS导入文本文件时,怎样跳过文本文件的第一行和最后一行
- 输入一个整数矩阵,计算位于矩阵边缘的元素之和。所谓矩阵边缘的元素,就是第一行和最后一行的元素以及第一列和最后一列的元素。
- listView优化,是否滑动,第一行,最后一行判断,判断何时要加载图片
- 向DataGridView中添加新的一行数据,可以添加到最后一行或作为第一行
- mysql按行进行查询( 查询第几行到第几行记录 查询最后一行和第一行记录 查询前几行和后几行记录)