对文件中的行进行排序并再次输出排序的行

时间:2010-08-25 18:06:57

标签: macos bash sorting command-line awk

我想逐行对文件中的行进行排序,我希望ouptut是按字母顺序排序的行。

例如:

queue list word letter gum  
another line of example words  
...

我希望输出为:

gum letter list queue word  
another example line of words  
...  

我似乎无法通过命令行

让它工作

我可能忽略了一些事情

4 个答案:

答案 0 :(得分:2)

如果安装了perl:

perl -ne 'print join " ", sort split /\s/ ; print "\n"'

EX:

cat input | perl -ne 'print join " ", sort split /\s/ ; print "\n"' > output

答案 1 :(得分:1)

如果包含单词列表的文件是foo.txt

while read line; do
  echo $(for w in $(echo "$line"); do echo "$w"; done |sort);
done < foo.txt

答案 2 :(得分:0)

这对我有用:

while read line
do
echo $line | tr " " "\n" | sort | tr "\n" " " ;echo
done < "input"

这个想法是:

  • 逐行阅读文件
  • 为每行读取,替换空格 使用换行符
  • 对结果列表进行排序
  • 用空格替换换行符和
  • 打印

答案 3 :(得分:0)

仅限awk:

gawk '{
    split($0, a)
    asort(a)
    for (i=1; i<=NF; i++) printf("%s ", a[i])
    print ""
}' infile