Bash 排序和多字符制表符错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24587955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:47:52  来源:igfitidea点击:

Bash sort and multi-character tab error

linuxbashsorting

提问by Andrej

I have data in the following form

我有以下形式的数据

C1510438;;C0220832;;2
C0026030;;C0034693;;1
C1257960;;C0007452;;1
C0061461;;C0027922;;2
C0011744;;C0037494;;3
C0014180;;C0034493;;3

When I try to sort on the 3rd field, the command returns the error

当我尝试对第三个字段进行排序时,该命令返回错误

sort -t ';;' -k 3 -r -n -o output.txt input.txt
sort: multi-character tab `;;'

I also try with

我也尝试

sort -t $';;' -k 3 -r -n -o output.txt input.txt

but the command returns same error.

但该命令返回相同的错误。

Any idea what to do?

知道该怎么做吗?

回答by julienc

The -toption expects a single separator character, but you give it two. A way to do what you want would be to consider that the separator is only a single ;, and thus the third column would become the fifth one:

-t选项需要一个分隔符,但您给它两个。做你想做的一种方法是考虑分隔符只是一个;,因此第三列将成为第五列:

sort -t ';' -k 5 -r -n -o output.txt input.txt

回答by GrnMtnBuckeye

Since the -t option expects a single separator character, a good way to handle this would be to use a replace tool to temporarily replace the separator(s) with a new one, do the sort, and then restore the original separator(s) as needed for further processing. I have a file that uses "," as separator which I can temporarily replace with a | (pipe), do my sort, and then restore "," as separator.

由于 -t 选项需要单个分隔符,因此处理此问题的一个好方法是使用替换工具将分隔符临时替换为新的分隔符,进行排序,然后恢复原始分隔符根据进一步处理的需要。我有一个使用“,”作为分隔符的文件,我可以暂时用 | 替换它。(管道),做我的排序,然后恢复“,”作为分隔符。