Text-Processing
基於文件的特定列的非字典排序,該列是製表符分隔的
file.txt
是製表符分隔:RollNo Names Class Subject Position 101 Anna V Maths Average 102 Bob V Maths Good 103 Charles VI Science Good 104 Darwin VI Science Improve 105 Eva VII English Improve
我想對這些行進行排序,以便它們按順序顯示
Good
,,Average
:Improve
RollNo Names Class Subject Position 102 Bob V Maths Good 103 Charles VI Science Good 101 Anna V Maths Average 104 Darwin VI Science Improve 105 Eva VII English Improve
給定
file.txt
:RollNo Names Class Subject Position 101 Anna V Maths Average 102 Bob V Maths Good 103 Charles VI Science Good 104 Darwin VI Science Improve 105 Eva VII English Improve
用數字替換該行的最後一個單詞。使用這些數字進行排序。然後將這些替換為原始單詞:
$ sed -e 's/Good$/1/' -e 's/Average$/2/' -e 's/Improve$/3/' file.txt | sort -k5n | sed -e 's/1$/Good/' -e 's/2$/Average/' -e 's/3$/Improve/' RollNo Names Class Subject Position 102 Bob V Maths Good 103 Charles VI Science Good 101 Anna V Maths Average 104 Darwin VI Science Improve 105 Eva VII English Improve
或者,根據該行的最後一個單詞在每行前面加上一個數字,然後按該數字排序。然後刪除第一列:
$ awk 'NR==1 {n=0} $NF=="Good" {n=1} $NF=="Average" {n=2} $NF=="Improve" {n=3} { print n, $0 }' file.txt | sort -n | cut -d' ' -f2- RollNo Names Class Subject Position 102 Bob V Maths Good 103 Charles VI Science Good 101 Anna V Maths Average 104 Darwin VI Science Improve 105 Eva VII English Improve