0

I have a CSV file filename.csv with the contents.

,"Range 1 (%)","Range 2 (%)"
"Color","Number Color 1","Number Color 2"
"Red","99.0","5.2"
"Orange","12.9","0.0"
"Yellow","33.9","1.2"
"Green","13.9","76.2"
"Blue","87.6","97.2"
"Purple","86.8","55.5"

I am trying to sort the file numerically by values from the second column. However, the first two rows of the file are the titles, and I would like to omit them. My command is below, but it outputs the original file without the first two rows, but also without sorting by the second column.

awk -F ',' '(NR>2)’ filename.csv | sort -t',' -k2 -n

Any help is appreciated :) Thanks!

2

1 Answer 1

1

As a variant:

head -n 2 file.csv && tail -n +3 file.csv | sort -k 2

Just process title rows and data rows separately.

If you want to pass the result into something else, you can do parentheses:

(head -n 2 file.csv && tail -n +3 file.csv | sort -k 2) | less
2
  • Hi, thanks for your response. That command isn't sorting the second column for me. Is there something that I am doing wrong?
    – c200402
    Mar 15, 2022 at 17:48
  • It sorts rows by second column. Not the second column alone, if that is what you want.
    – White Owl
    Mar 15, 2022 at 21:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .