Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使用sort-M按月排序_Bash_Sorting_Unix_Cat_Utilities - Fatal编程技术网

Bash 使用sort-M按月排序

Bash 使用sort-M按月排序,bash,sorting,unix,cat,utilities,Bash,Sorting,Unix,Cat,Utilities,我有一个文件months.txt,其中包含以下文本: JAN, MAR, DEC, FEB, JUN, APR 在bash中,我编写了以下代码行: cat months.txt | sort -M 我假设这将输出文本文件,按月份排序。但是,输出没有排序。我是否使用了排序错误?sort对文本行进行排序,而您只给了它一行 如果您的文件如下所示: JAN MAR DEC FEB JUN APR 。。。你会得到你期望的结果 顺便说一句,您的命令是a:the命令 sort -M months.txt

我有一个文件months.txt,其中包含以下文本:

JAN, MAR, DEC, FEB, JUN, APR
在bash中,我编写了以下代码行:

cat months.txt | sort -M
我假设这将输出文本文件,按月份排序。但是,输出没有排序。我是否使用了排序错误?

sort
对文本行进行排序,而您只给了它一行

如果您的文件如下所示:

JAN
MAR
DEC
FEB
JUN
APR
。。。你会得到你期望的结果

顺便说一句,您的命令是a:the命令

sort -M months.txt
执行完全相同的操作。

排序
对文本行进行排序,而您只给了它一行

如果您的文件如下所示:

JAN
MAR
DEC
FEB
JUN
APR
。。。你会得到你期望的结果

顺便说一句,您的命令是a:the命令

sort -M months.txt

执行完全相同的操作。

您需要将逗号分隔的列表转换为单独的行,对其进行排序,然后将其转换回:

tr ',' '\n' < months.txt | sort -M | awk 'NR > 1 { printf(",") } {printf("%s", $0)} END { print "" }'
tr','''\n'1{printf(“,”)}{printf(“%s”,$0)}END{print”“}

您需要将逗号分隔的列表转换为单独的行,对其进行排序,然后将其转换回:

tr ',' '\n' < months.txt | sort -M | awk 'NR > 1 { printf(",") } {printf("%s", $0)} END { print "" }'
tr','''\n'1{printf(“,”)}{printf(“%s”,$0)}END{print”“}
另一个镜头:

tr -s ', ' '\n\n' < months.txt | sort -M | paste -sd,
输出中没有空格。

另一个镜头:

tr -s ', ' '\n\n' < months.txt | sort -M | paste -sd,

输出中没有空格。

排序
按行排序。您所有的月份都在同一行。
排序
按行排序。你所有的月份都在同一条线上。