bash sort / uniq -c:如何在输出中使用制表符而不是空格作为分隔符?

I Z*_*I Z 4 sorting bash delimiter

我有一个strings.txt列出字符串的文件,我正在这样处理:

sort strings.txt | uniq -c | sort -n > uniq.counts
Run Code Online (Sandbox Code Playgroud)

因此,生成的文件uniq.counts将列出按计数升序排序的 uniq 字符串,如下所示:

 1 some string with    spaces
 5 some-other,string
25 most;frequent:string
Run Code Online (Sandbox Code Playgroud)

请注意,字符串中strings.txt可能包含空格、逗号、分号和其他分隔符(制表符除外)。我怎样才能uniq.counts成为这种格式:

 1<tab>some string with    spaces
 5<tab>some-other,string
25<tab>most;frequent:string
Run Code Online (Sandbox Code Playgroud)

anu*_*ava 5

你可以做:

sort strings.txt | uniq -c | sort -n | sed -E 's/^ *//; s/ /\t/' > uniq.counts
Run Code Online (Sandbox Code Playgroud)

sed将首先删除行开头(计数之前)的所有前导空格,然后将计数后的空格替换为tab字符。