Bob
Bob

Reputation: 4970

bash - how do you sort within the lines of a text file

Using the linux command sort, how do you sort the lines within a text file?

Normal sort swaps the lines until they're sorted while I want to swap the words within the lines until they're sorted.

Example:

Input.txt

z y x v t
c b a

Output.txt

t v x y z
a b c

Upvotes: 2

Views: 6130

Answers (6)

sebbit
sebbit

Reputation: 121

Having tried various ways to solve this (involving, e.g., GNU parallel and/or xargs) I found a simple way to do this, using only GNU coreutils:

split -l 1 --filter 'tr " " "\n" | sort | paste -s -d " "' \
in.txt > out.txt

Unfortunately, I think the --filter option to split is a GNU addition...

Upvotes: 0

RoGoR
RoGoR

Reputation: 11

I was looking for a magic switch but found my own solution more intuitive:

$ line="102 103 101 102 101"
$ echo $(echo "${line}"|sed 's/\W\+/\n/g'|sort -un)
101 102 103

Thank you!

Upvotes: 1

janos
janos

Reputation: 124646

To sort words within lines using sort, you would need to read line by line, and call sort once for each line. It gets quite tricky though, and in any case, running one sort process for each line wouldn't be very efficient.

You could do better by using Perl (thanks @glenn-jackman for the awesome tip!):

perl -lape '$_ = qq/@{[sort @F]}/' file

Upvotes: 4

carl.anderson
carl.anderson

Reputation: 1118

Here's a fun way that actually uses the linux sort command (plus xargs):

while read line; do xargs -n1 <<< $line | sort | xargs; done < input.txt

Now, this makes several assumptions (which are probably not always true), but the main idea is xargs -n1 takes all the tokens in a line and emits them on separate lines in stdout. This output gets piped through sort and then a final xargs with no arguments puts them all back into a single line.

Upvotes: 5

anubhava
anubhava

Reputation: 785068

If you have gnu awk then it can be done in a single command using asort function:

awk '{for(i=1; i<=NF; i++) c[i]=$i; n=asort(c); 
for (i=1; i<=n; i++) printf "%s%s", c[i], (i<n?OFS:RS); delete c}' file

t v x y z
a b c

Upvotes: 3

ghoti
ghoti

Reputation: 46836

It's a little awkward, but this uses only a basic sort command, so it's perhaps a little more portable than something that requires GNU sort:

while read -r -a line; do
  printf "%s " $(sort <<<"$(printf '%s\n' "${line[@]}")")
  echo
done < input.txt

The echo is included to insert a newline, which printf doesn't include by default.

Upvotes: 0

Related Questions