Bash-如何在文本文件的行内进行排序

人气:543 发布:2022-10-16 标签: text-files sorting bash

问题描述

使用Linux命令sort,如何对文本文件中的行进行排序?

Normalsort交换行直到它们被排序,而我想交换行中的单词直到它们被排序。

示例:

Input.txt

z y x v t
c b a

Output.txt

t v x y z
a b c

推荐答案

如果您有gnu awk,则可以使用asort function在一个命令中完成:

awk '{for(i=1; i<=NF; i++) c[i]=$i; n=asort(c); 
for (i=1; i<=n; i++) printf "%s%s", c[i], (i<n?OFS:RS); delete c}' file

t v x y z
a b c

667