0

How to arrange in ascending order from left to right in vi or with the sort command? Sample before arrangement:

2  3
3  6
5  1
4  2
5  8

I want it in this way:

2  3
3  6
1  5
2  4
5  8
  • while read p;do echo $p|tr ' ' '\n'|sort -k1|paste -s -d' ' -;done<input>output – krowe Sep 30 '14 at 10:27
  • @krowe Go ahead – slhck Sep 30 '14 at 11:06
0

This can be one lined with the following command:

while read p;do echo $p|tr ' ' '\n'|sort -k1|paste -s -d' ' -;done<input>output

Replace input and output with your actual IO files.

I wrote a small script which can be used to test this as well:

#!/bin/bash
# This script is just for testing purposes. 
# Call this like:

#   sort_numbers numbers.txt numbers_correct.txt

# Where numbers.txt is the file to sort and
# numbers_correct.txt is a file where they are 
# sorted correct.

while read p; do 
  echo $p|tr ' ' '\n'|sort -k1|paste -s -d' ' -
done<$1>output.$1

# This just compares the output with the correct output
file1=$2
file2=output.$1
echo -n $file1 and $file2 are' ' && cmp --silent $file1 $file2 && echo the same || echo different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.