按第二列排序文件

时间:2014-04-16 21:09:34

标签: bash shell unix

我试图按字母顺序对每行的第二列进行排序,其中包含大写名称。示例文件:

1:JOHN:Morgan:90:24
2:MIKE:Smith:95:11
3:JAYSON:Ty:99:9
4:TYLER:Edward:89:5

这是我的bash脚本:

file="/home/here.txt"

while IFS=: read -r f1 f2 f3 f4 f5
do
        tput cup $f1 0 ;echo  "$f2 $f1 $f3 $f4 $f5"
done <"$file"

我来到这里对它们进行排序,但这只能用f1切换f2的位置。

1 个答案:

答案 0 :(得分:3)

您可以使用此按字母顺序按文件的第二列对文件进行排序:

file="/home/here.txt"
sort -t":" -k2 $file; # -t is delimeter and -k is column number to sort

阅读手册页中的排序以获取更多信息。

相关问题