I have seen lot of posts on sorting a file based on a column but didn't help me.
I want to sort a CSV file based on column 2 only.
for example : The data in my file looks like below
H1,H2,H3,H4
C11,R_G,S_F_G,22-OCT-2019
C12,R_G,S_F_G,22-OCT-2019
C13,R_E,S_F_E,22-OCT-2019
C13,R_E,S_F_E_RA,22-OCT-2019
C13,R_E,S_F_E_RB,22-OCT-2019
C14,R_E,S_F_E,22-OCT-2019
C14,R_E,S_F_E_RA,22-OCT-2019
C14,R_E,S_F_E_RB,22-OCT-2019
Expected sorting order of column 2 like below
H1,H2,H3,H4
C13,R_E,S_F_E,22-OCT-2019
C13,R_E,S_F_E_RA,22-OCT-2019
C13,R_E,S_F_E_RB,22-OCT-2019
C14,R_E,S_F_E,22-OCT-2019
C14,R_E,S_F_E_RA,22-OCT-2019
C14,R_E,S_F_E_RB,22-OCT-2019
C11,R_G,S_F_G,22-OCT-2019
C12,R_G,S_F_G,22-OCT-2019
I tried to sort with below command
awk 'NR==1; NR > 1 {print $0 | "sort -t, -k2"}' inputfile >> outputfile
The result is like below
H1,H2,H3,H4
C13,R_E,S_F_E,22-OCT-2019
C14,R_E,S_F_E,22-OCT-2019
C13,R_E,S_F_E_RA,22-OCT-2019
C14,R_E,S_F_E_RA,22-OCT-2019
C13,R_E,S_F_E_RB,22-OCT-2019
C14,R_E,S_F_E_RB,22-OCT-2019
C11,R_G,S_F_G,22-OCT-2019
C12,R_G,S_F_G,22-OCT-2019
If you observe the result, column 2 and column 3 is getting sorted, but I want only column 2 to be sorted and if the column 2 is same for 2 rows it should be in the order of appearance in the input file.
it will be great if anyone can help me to understand what I am doing wrong