cut中的定界符是什么意思,为什么在此命令中它会排序两次? [英] what is the meaning of delimiter in cut and why in this command it is sorting twice?

查看:122
本文介绍了cut中的定界符是什么意思,为什么在此命令中它会排序两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找此命令的原因,据我所知,我很基本地发现了这一点

I am trying to find the reason of this command and as I know very basic I found that

last | cut -d" " -f 1 | sort | uniq -c | sort

last =最后一次在文件/var/log/wtmp(或-f标志指定的文件)中进行搜索,并显示自创建该文件以来所有已登录(和退出)的用户的列表.

last = Last searches back through the file /var/log/wtmp (or the file designated by the -f flag) and displays a list of all users logged in (and out) since that file was created.

cut用于显示所需的列.

选项-d指定在输入文件中使用的字段分隔符.

The option -d specifies what is the field delimiter that is used in the input file.

-f指定要提取的字段

1是我认为不确定的输出

1 is the out put I think which I am not sure

它正在排序,然后是

Uniq命令有助于删除或检测文件中的重复条目.本教程介绍了一些可能会有所帮助的最常用的uniq命令行选项.

Uniq command is helpful to remove or detect duplicate entries in a file. This tutorial explains few most frequently used uniq command line options that you might find helpful.

如果任何人都可以解释此命令并解释为什么有两种,我将不胜感激.

If anyone can explain this command and also explain why there is two sorts I will appreciate it.

推荐答案

您正确地解释了cut:cut -d" " -f1(在f之后不需要空格)获得了第一个f ield基于d elimiter " "(空格)的流.

You are right on your explanation of cut: cut -d" " -f1 (no need of space after f) gets the first field of a stream based on delimiter " " (space).

那为什么要sort | uniq -c | sort?

来自man uniq:

注意:'uniq'不会检测重复的行,除非它们相邻. 您可能要先对输入进行排序,或者使用不带"uniq"的"sort -u". 另外,比较会遵循"LC_COLLATE"指定的规则.

Note: 'uniq' does not detect repeated lines unless they are adjacent. You may want to sort the input first, or use 'sort -u' without 'uniq'. Also, comparisons honor the rules specified by 'LC_COLLATE'.

这就是为什么在管道输送到uniq之前需要对行进行排序的原因.最后,由于未对uniq输出进行排序,因此您需要再次排序以首先查看重复次数最多的项.

That's why you need to sort the lines before piping to uniq. Finally, as uniq output is not sorted, you need to sort again to see the most repeated items first.

对于包含重复项的给定文件,请参见sortuniq -c的示例:

See an example of sort and uniq -c for a given file with repeated items:

$ seq 5 >>a
$ seq 5 >>a
$ cat a
1
2
3
4
5
1
2
3
4
5

$ sort a | uniq -c | sort <--- no repeated matches
      2 1
      2 2
      2 3
      2 4
      2 5

$ uniq -c a | sort <---- repeated matches
      1 1
      1 1
      1 2
      1 2
      1 3
      1 3
      1 4
      1 4
      1 5
      1 5


请注意,您可以同时执行sort | uniq -c与此awk:


Note you can do the sort | uniq -c all together with this awk:

last | awk '{a[$1]++} END{for (i in a) print i, a[i]}'

这会将第一列的值存储在a[]数组中,并在发现更多内容时增加计数器.在END{}块中,它会打印未排序的结果,因此您可以再次通过管道传输到sort.

This will store in the a[] array the values of the first column and increase the counter whenever it finds more. In the END{} blocks it prints the results, unsorted, so you could pipe again to sort.

这篇关于cut中的定界符是什么意思,为什么在此命令中它会排序两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆