Richard Parsons
Richard Parsons

Reputation: 277

sort multiple tabs but ignoring spaces

I have a data file like this (\t represents tabs):

short line\t    \t    \t    \t    \t    3
very long line with lots of text\t\t    2

How could I sort it by the second column using sort? In other words I want to set the delimiter to be multiple tabs, but not spaces.

Upvotes: 1

Views: 1547

Answers (3)

glenn jackman
glenn jackman

Reputation: 247042

It seems that the field separator for sort must be a single character, so this command:

sort -t $'\t' -k2 file

will not handle multiple tabs as a single separator: it will sort the empty 2nd field for both lines.

This command will successfully find the the second field, but it modifies the text:

tr -s '\t' < file | sort -t $'\t' -k2

Note that tr interprets the 2-character string "\t" as a tab character, while sed -t does not. Just a foible of how different commands are implemented.

Upvotes: 1

gw0
gw0

Reputation: 1593

Setting the field delimiter to something else is accomplished with the -t parameter. But passing a tab character can be tricky, so the solution may look like:

sort -t "$(echo -e '\t')" -k 2 file.txt

Upvotes: 0

sehe
sehe

Reputation: 393557

sort -k2 -t'        ' test.txt

worked out of the box for me. Enter the TAB inside ' ' as C-vTab in bash

Upvotes: 0

Related Questions