0

I would like to sort the lines of a data file (each line idependent from each other) from the first character. For example, if I have a data file

1   0.1     0.6     0.4
2   0.5     0.2     0.3
3   1.0     0.2     0.8

I would like to end with something like

1   0.6     0.4     0.1
2   0.5     0.3     0.2
3   1.0     0.8     0.2

I have tried to do it using the sort command, but it sorts the columns (not the line). Transposing the data file +sort could be also a good solution (I don't know any easy way for transposing datafiles).

Thanks for the help!

1

2 Answers 2

2

Perl to the rescue!

perl -lawne '
    print join "\t", $F[0], sort { $b <=> $a } @F[1..$#F]
' < input > output
  • -n reads the input line by line
  • -a splits the line on whitespace into the @F array
  • -l adds newlines to print

See sort, join .

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

1 Comment

So useful and powerful command. Thank you for your answer, it definitively solved my problem!
1

Or to read input line by line, use tr and sort like this:

#! /bin/sh
while read -r line; do
    echo $line | tr ' ' '\n' | sort -k1,1nr -k2 | tr '\n' '\t' >> output
    echo >> output
done < input

tr ' ' '\n' is to convert row to column.

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.