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)
你可以做:
sort strings.txt | uniq -c | sort -n | sed -E 's/^ *//; s/ /\t/' > uniq.counts
Run Code Online (Sandbox Code Playgroud)
sed
将首先删除行开头(计数之前)的所有前导空格,然后将计数后的空格替换为tab
字符。