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 ?

3
  • What's the sample output? Commented Sep 8, 2017 at 0:03
  • 1
    cat file |sed 's/_/ /g' |sort -k2 |sed -r 's/\s+/_/' Commented Sep 8, 2017 at 0:43
  • 3
    Alternatively, sort -t_ -k2 should work. Commented Sep 8, 2017 at 0:44

1 Answer 1

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

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.