3

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
0

6 Answers 6

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

7 Comments

With echo $(printf ... | sort), you risk glob expansion, no? You could just do printf ... | sort instead.
$line itself is subject to pathname expansion.
@BenjaminW. the wrapping in echo $(...) is to turn the multiline result of sort back into a single line
Nope, now you are only writing a single line to sort, because "$line" isn't subject to word-splitting.
Can be written as perl -lape '$_="@{[sort @F]}"' file
|
3

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

Comments

1

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!

Comments

0

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.

2 Comments

This is subject to globbing: if I have files f1.txt and f2.txt in the current directory and one of the words in input.txt is f?.txt, it'll expand to f1.txt f2.txt.
Something like sort <<< "$(printf '%s\n' "${line[@]}")" | paste -s -d ' ' might work, assuming that the words are separated by a single space each.
0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.