Sample Header Ad - 728x90

Sorting a table with a non-newline record separator in bash

0 votes
1 answer
610 views
I have a table that looks like 'a;b;f|d;e;c|g;h' which I wish to sort by the third column. The output should be 'g;h|d;e;c|a;b;f' If I were to use the standard delimiters whitespace and newline then this could be accomplished using standard sort: printf '%b' 'a b f\nd e c\ng h' | sort -k 3 would output g h d e c a b f The sort command can also take a non standard field separator with the -t option: printf '%b' "a;b;f\nd;e;c\ng;h" | sort -k 3 -t ';' would output g;h d;e;c a;b;f I have however failed to find a way to sort a table with a non-newline record separator. Can this be accomplished? If so, how? Edit ---- A key condition is to alter neither data nor delimiters in the process, only the order in which they appear. Other tools than the sort command are also ok. Preferably POSIX compliant, but not necessarily.
Asked by fuumind (449 rep)
Oct 27, 2021, 07:10 AM
Last activity: Oct 27, 2021, 08:14 AM