需要使用时,与从输出删除计数QUOT; uniq的-c"命令 [英] Need to remove the count from the output when using "uniq -c" command
问题描述
我想读一个文件,并通过特定字段的出现次数排序。假设我想找出从日志文件,然后我用的uniq -c选项,它降序排列最重复的日期。像这样
I am trying to read a file and sort it by number of occurrences of a particular field. Suppose i want to find out the most repeated date from a log file then i use uniq -c option and sort it in descending order. something like this
uniq -c | sort -nr
这会产生一定的输出这样的 -
This will produce some output like this -
809 23/Dec/2008:19:20
这实际上是计数的第一场是我的问题....我想ONY从上面的输出日期,但中号没能得到这一点。我试图用剪切命令,这样做,
the first field which is actually the count is the problem for me .... i want to get ony the date from the above output but m not able to get this. I tried to use cut command and did this
uniq -c | sort -nr | cut -d' ' -f2
但这只是打印空白...请有人可以帮我只得到的日期和砍掉计数。我只希望
but this just prints blank space ... please can someone help me on getting the date only and chop off the count. I want only
23/Dec/2008:19:20
感谢
推荐答案
从 uniq的
计数是由空格pceded除非有超过7位在$ p $算,所以你需要做的是这样的:
The count from uniq
is preceded by spaces unless there are more than 7 digits in the count, so you need to do something like:
uniq -c | sort -nr | cut -c 9-
要获得列(字符位置)9向上。或者你可以使用 SED
:
to get columns (character positions) 9 upwards. Or you can use sed
:
uniq -c | sort -nr | sed 's/^.\{8\}//'
或
uniq -c | sort -nr | sed 's/^ *[0-9]* //'
这第二个选项是在1000万以上的重复次数面对强大的;如果你认为这可能是一个问题,它可能是比剪切
替代更好。而且有可用的无疑是其他的选择了。
This second option is robust in the face of a repeat count of 10,000,000 or more; if you think that might be a problem, it is probably better than the cut
alternative. And there are undoubtedly other options available too.
注意:这个计数通过实验在Mac OS X 10.7.3确定,但使用GNU uniq的
从的coreutils
8.3。在BSD 的uniq -c
生产前一个数字计数3前导空格。该POSIX规范说从 的uniq -c
<输出/ A>的格式必须象用:
Caveat: the counts were determined by experimentation on Mac OS X 10.7.3 but using GNU uniq
from coreutils
8.3. The BSD uniq -c
produced 3 leading spaces before a single digit count. The POSIX spec says the output from uniq -c
shall be formatted as if with:
printf("%d %s", repeat_count, line);
这不会有任何前导空白。鉴于输出格式这种可能的变化,用 SED
脚本 [0-9]
正则表达式是最可靠的方法在观察和理论产量的变化处理从的uniq -c
:
which would not have any leading blanks. Given this possible variance in output formats, the sed
script with the [0-9]
regex is the most reliable way of dealing with the variability in observed and theoretical output from uniq -c
:
uniq -c | sort -nr | sed 's/^ *[0-9]* //'
这篇关于需要使用时,与从输出删除计数QUOT; uniq的-c&QUOT;命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!