我有一个包含以下行的文件:
This test took 1201ms to execute
The IO operation cost 113ms
Main thread have been executing for 16347ms
如何按ms
旁边的数字对其进行排序?
我使用了以下sed
命令,但没有工作
sed -r 's/[[:digit]]\+ms//g' file.txt | sort -r | > tmp
答案 0 :(得分:2)
$ awk '{match($0,/[[:digit:]]+ms/,a)}{print substr(a[0], 1, length(a[0])-2),$0}' inputFile | sort -nk1 | cut -f2- -d ' '
The IO operation cost 113ms
This test took 1201ms to execute
Main thread have been executing for 16347ms
awk
与[[:digit:]]ms
匹配,并将其(最后两个字符ms
除外)打印到该行的开头,并使用第一个字段打印sort
。 cut
稍后删除第一个字段并返回原始行。
答案 1 :(得分:2)
GNU awk
:
awk 'BEGIN {PROCINFO["sorted_in"]="@ind_num_asc"} \
{idx=gensub(".*\\s+([0-9]+).*", "\\1", "g"); arr[idx]=$0} \
END{for (i in arr) print arr[i]}' file.txt
PROCINFO["sorted_in"]="@ind_num_asc"
变量设置基于数字索引的(关联)数组排序顺序
{idx=gensub(".*\\s+([0-9]+).*", "\\1", "g"); arr[idx]=$0}
获取数字并使其成为关联数组arr
的索引,其值为相应的记录
END{for (i in arr) print arr[i]}
打印数组值
如果要将排序顺序反转为降序,请执行:
PROCINFO["sorted_in"]="@ind_num_desc"
示例:强>
% cat file.txt
This test took 1201ms to execute
The IO operation cost 113ms
Main thread have been executing for 16347ms
% awk 'BEGIN {PROCINFO["sorted_in"]="@ind_num_asc"} {idx=gensub(".*\\s+([0-9]+).*", "\\1", "g"); arr[idx]=$0} END{for (i in arr) print arr[i]}' file.txt
The IO operation cost 113ms
This test took 1201ms to execute
Main thread have been executing for 16347ms
答案 2 :(得分:1)
使用GNU awk(gawk):
$ awk 'BEGIN{PROCINFO["sorted_in"]="@val_num_asc"} {for (i=1;i<=NF;i++) if ($i~/ms$/){a[$0]=$i+0; break}} END{for (line in a)print line}' file.txt
The IO operation cost 113ms
This test took 1201ms to execute
Main thread have been executing for 16347ms
BEGIN{PROCINFO["sorted_in"]="@val_num_asc"}
这告诉awk按数组值按升序对数组进行排序。这是一个GNU功能。
for (i=1;i<=NF;i++) if ($i~/ms$/){a[$0]=$i+0; break}
对于某一行中的每个字段,我们会看到它是否以ms
结尾。如果是,我们将关键数组a
分配给等于整行的键下该字段的值。
END{for (line in a)print line}
在我们读完整个文件后,我们打印出数组a
的键。由于数组a
按值按升序排序,因此打印输出按时间按升序排列。
答案 3 :(得分:1)
您可以使用sed
提取数字部分,并使用分隔符将其放在行的开头,然后使用sort
将其放在第一个字段中,并使用cut
删除添加的字段:
sed -E 's/^(.*) ([[:digit:]]+)ms(.*)$/\2|\1 \2ms\3/' file | # extract ms and place it at the beginning
sort -t '|' -k1,1n | # sort it by the field added above
cut -f2- -d '|' # remove the field
输出:
The IO operation cost 113ms
This test took 1201ms to execute
Main thread have been executing for 16347ms