【发布时间】:2016-07-12 19:13:21
【问题描述】:
我有一个文件strings.txt 列出字符串,我正在处理它:
sort strings.txt | uniq -c | sort -n > uniq.counts
因此生成的文件uniq.counts 将列出按计数升序排序的 uniq 字符串,如下所示:
1 some string with spaces
5 some-other,string
25 most;frequent:string
请注意,strings.txt 中的字符串可能包含空格、逗号、分号和其他分隔符,制表符除外。我怎样才能让uniq.counts 采用这种格式:
1<tab>some string with spaces
5<tab>some-other,string
25<tab>most;frequent:string
【问题讨论】:
-
这不是关于
sort的真正问题(或者,更确切地说,更改sort使用的分隔符既简单又显示在手册页中,如-taka--field-separator; 因此sort -t $'\t'足以回答原标题中提出的全部问题);有趣的部分是如何将uniq -c使用的分隔符更改为制表符。 -
正如 chepner 简要评论的那样——即使在默认情况下使用 IFS,
while read -r count content; do ...也可以使用原始输出格式成功解析来自uniq.counts中其余输出的计数,而无需单独字符。 -
这能回答你的问题吗? Why uniq -c output with space instead of \t?