awk对两个字段进行排序:

时间:2017-09-14 16:30:56

标签: unix awk

希望根据字段Input.csv$1 and $5文件进行排序,并生成国家/地区A-Z order。 在进行排序时,如果任何字段为空,则需要考虑$ 1或$ 5的国家/地区名称。

Input.csv

Country,Amt,Des,Details,Country,Amt,Des,Network,Details
abc,10,03-Apr-14,Aug,abc,10,DL,ABC~XYZ,Sep
,,,,mno,50,DL,ABC~XYZ,Sep
abc,10,22-Jan-07,Aug,abc,10,DL,ABC~XYZ,Sep
jkl,40,11-Sep-13,Aug,,,,,
,,,,ghi,30,AL,DEF~PQZ,Sep
abc,10,03-Apr-14,Aug,abc,10,MN,ABC~XYZ,Sep
abc,10,19-Feb-14,Aug,abc,10,MN,ABC~XYZ,Sep
def,20,02-Jul-13,Aug,,,,,
def,20,02-Aug-13,Aug,,,,,

Desired Output.csv

Country,Amt,Des,Details,Country,Amt,Des,Network,Details
abc,10,03-Apr-14,Aug,abc,10,DL,ABC~XYZ,Sep
abc,10,22-Jan-07,Aug,abc,10,DL,ABC~XYZ,Sep
abc,10,03-Apr-14,Aug,abc,10,MN,ABC~XYZ,Sep
abc,10,19-Feb-14,Aug,abc,10,MN,ABC~XYZ,Sep
def,20,02-Jul-13,Aug,,,,,
def,20,02-Aug-13,Aug,,,,,
,,,,ghi,30,AL,DEF~PQZ,Sep
jkl,40,11-Sep-13,Aug,,,,,
,,,,mno,50,DL,ABC~XYZ,Sep

我已尝试过以下命令但未获得所需的输出。请建议..

head -1 Input.csv > Output.csv;  sort -t, -k1,1 -k5,5 <(tail -n +2 Input.csv) >> Output.csv

3 个答案:

答案 0 :(得分:3)

awk救援!

$ awk -F, '{print ($1==""?$5:$1) "\t" $0}' file | sort | cut -f2-

Country,Amt,Des,Details,Country,Amt,Des,Network,Details
abc,10,03-Apr-14,Aug,abc,10,DL,ABC~XYZ,Sep
abc,10,03-Apr-14,Aug,abc,10,MN,ABC~XYZ,Sep
abc,10,19-Feb-14,Aug,abc,10,MN,ABC~XYZ,Sep
abc,10,22-Jan-07,Aug,abc,10,DL,ABC~XYZ,Sep
def,20,02-Aug-13,Aug,,,,,
def,20,02-Jul-13,Aug,,,,,
,,,,ghi,30,AL,DEF~PQZ,Sep
jkl,40,11-Sep-13,Aug,,,,,
,,,,mno,50,DL,ABC~XYZ,Sep

此处标题以大写开头,数据为小写。如果这不是一个有效的假设,就像你上面所做的那样需要对标题进行特殊处理,或者更好地使用awk

$ awk -F, 'NR==1{print; next} {print ($1==""?$5:$1) "\t" $0 | "sort | cut -f2-"}' file

答案 1 :(得分:0)

这是你想要的吗? (省略第一行)

cat file_containing_your_lines | awk 'NR != 1'  | sed "s/,/\t/g" | sort -k 1 -k 5 | sed "s/\t/,/g"

答案 2 :(得分:0)

请您试试,请告诉我这是否对您有所帮助。头,尾,排序解决方案:

head -n 1  Input_file && tail -n +2  Input_file | sort -k1.1 -k5.1

输出如下。

Country,Amt,Des,Details,Country,Amt,Des,Network,Details
abc,10,03-Apr-14,Aug,abc,10,DL,ABC~XYZ,Sep
abc,10,03-Apr-14,Aug,abc,10,MN,ABC~XYZ,Sep
abc,10,19-Feb-14,Aug,abc,10,MN,ABC~XYZ,Sep
abc,10,22-Jan-07,Aug,abc,10,DL,ABC~XYZ,Sep
def,20,02-Aug-13,Aug,,,,,
def,20,02-Jul-13,Aug,,,,,
,,,,ghi,30,AL,DEF~PQZ,Sep
jkl,40,11-Sep-13,Aug,,,,,
,,,,mno,50,DL,ABC~XYZ,Sep

如果您的区域设置未设置为en_US,请尝试使用sort

更改LC_ALL=en_US sort