I am trying to sort the rows of a text file in Unix Korn shell.
Sample File:
3|"Apple"|"iphoneX"|"This is the latest phone."|"California"
2|"Samsung"|"s9"|"This phone isn't so good.
It hangs for no reason when old."|"Texas"
1|"OnePlus"|"5t"|"This is a good android phone."|"Arizona"
Expected O/P:
1|"OnePlus"|"5t"|"This is a good android phone."|"Arizona"
2|"Samsung"|"s9"|"This phone isn't so good.
It hangs for no reason when old."|"Texas"
3|"Apple"|"iphoneX"|"This is the latest phone."|"California"
I tried this:
${FileDir}${INPUT_FILE} |sort -t '|' -k ${SORT_COL_ORD} >>${FileDir}${SORT_OUTPUT_FILE}
But it's failing due to a new line feed in row 2. And my O/P is something like this :
1|"OnePlus"|"5t"|"This is a good android phone."|"Arizona"
2|"Samsung"|"s9"|"This phone isn't so good.
3|"Apple"|"iphoneX"|"This is the latest phone."|"California"
It hangs for no reason when old."|"Texas"
I am passing an arg to sort on col 1 (Ids).What am I missing here?
sortsorts lines. You're going to need to transform the input so that single logical lines are on a single physical line. You might do that by replacing any occurrences of backslash with double backslash, any occurrences of newline in a logical line with\n, and then sort the data appropriately, and finally undo the transform. It is at least as much fiddly as difficult.It is wonderful except it has no "|" (pipe) character— how would that be encoded? At this point, something like Perl with the Text::CSV module(s) starts to be beneficial. Perl can sort, too.