Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash sort / uniq -c: how to use tab instead of space as delimiter in output?

I have a file strings.txt listing strings, which I am processing like this:

sort strings.txt | uniq -c | sort -n > uniq.counts

So the resulting file uniq.counts will list uniq strings sorted in the ascending order by their counts, so something like this:

 1 some string with    spaces
 5 some-other,string
25 most;frequent:string

Note that strings in strings.txt may contain spaces, commas, semicolons and other separators, except for the tab. How can I get uniq.counts to be in this format:

 1<tab>some string with    spaces
 5<tab>some-other,string
25<tab>most;frequent:string
like image 792
I Z Avatar asked Oct 18 '25 12:10

I Z


1 Answers

You can do:

sort strings.txt | uniq -c | sort -n | sed -E 's/^ *//; s/ /\t/' > uniq.counts

sed will first remove all leading spaces at the beginning of the line (before counts) and then it will replace space after count to tab character.

like image 142
anubhava Avatar answered Oct 21 '25 02:10

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!