linux - what is the meaning of delimiter in cut and why in this command it is sorting twice? -
i trying find reason of command , know basic found that
last | cut -d" " -f 1 | sort | uniq -c | sort
last
= last searches through file /var/log/wtmp (or file designated -f flag) , displays list of users logged in (and out) since file created.
cut
show desired column.
the option -d
specifies field delimiter used in input file.
-f
specifies field want extract
1 out put think not sure
and sorting , is
uniq
command helpful remove or detect duplicate entries in file. tutorial explains few used uniq command line options might find helpful.
if can explain command , explain why there 2 sorts appreciate it.
you right on explanation of cut
: cut -d" " -f1
(no need of space after f
) gets first f
ield of stream based on d
elimiter " "
(space).
then why sort | uniq -c | sort
?
from man uniq
:
note: 'uniq' not detect repeated lines unless adjacent. may want sort input first, or use 'sort -u' without 'uniq'. also, comparisons honor rules specified 'lc_collate'.
that's why need sort lines before piping uniq
. finally, uniq
output not sorted, need sort again see repeated items first.
see example of sort
, uniq -c
given file repeated items:
$ seq 5 >>a $ seq 5 >>a $ cat 1 2 3 4 5 1 2 3 4 5 $ sort | uniq -c | sort <--- no repeated matches 2 1 2 2 2 3 2 4 2 5 $ uniq -c | sort <---- repeated matches 1 1 1 1 1 2 1 2 1 3 1 3 1 4 1 4 1 5 1 5
note can sort | uniq -c
awk:
last | awk '{a[$1]++} end{for (i in a) print i, a[i]}'
this store in a[]
array values of first column , increase counter whenever finds more. in end{}
blocks prints results, unsorted, pipe again sort
.
Comments
Post a Comment