0

I have 1 csv file space delimited,

Jan_high 32 123
Febr_low 19 139
March_high 12 63
Jan_low 36 18
Febr_high 87 99
March_low 83 77

how do I sort it based on _high, then _low string 1st column? I cant use sort -k1.5 since the string high or low doesn't start at fixed character location. Does anyone have idea ?

  • What's the sample output? – Jeff Schaller Sep 8 '17 at 0:03
  • 1
    cat file |sed 's/_/ /g' |sort -k2 |sed -r 's/\s+/_/' – Emilio Galarraga Sep 8 '17 at 0:43
  • 3
    Alternatively, sort -t_ -k2 should work. – Jeremy Dover Sep 8 '17 at 0:44
0

Using -t '_' -k2 with sort would make sort consider the input as delimited on the _ character instead of on the spaces, and then sort on the second field (the one starting with either high or low):

$ sort -t '_' -k2 file
March_high 12 63
Jan_high 32 123
Febr_high 87 99
Febr_low 19 139
Jan_low 36 18
March_low 83 77

Taking the first line of input as example:

Jan_high 32 123
^^^ ^^^^^^^^^^^
f1  field2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.