开发者

sort multiple tabs but ignoring spaces

I have a data file like this (\t represents ta开发者_运维技巧bs):

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.


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.


sort -k2 -t'        ' test.txt

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜