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
  • Relating unix.stackexchange.com/questions/343720/… Commented Mar 15, 2022 at 12:31
  • If you have a CSV file, post the CSV file. Table formatting is neat and keen, but please don’t use it inappropriately. Also, for questions about how to process data, show the input and the desired output. Commented Mar 15, 2022 at 21:38

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? Commented Mar 15, 2022 at 17:48
  • It sorts rows by second column. Not the second column alone, if that is what you want. Commented Mar 15, 2022 at 21:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.