Is there any better way for sorting in bash/awk

Hi,

I have a file which is:-

1 6 4 8 2 3
2 1 9 3 2 1
3 3 5 6 3 1
4 9 7 8 2 3

I would like to sort from field $2 to field $6 for each of the line to:-

1 2 3 4 6 8
2 1 1 2 3 9
3 1 3 3 5 6
4 2 3 7 8 9

I came across this Arrays on example 26-6. But it is much complicated. I am looking any better way to perform the above. For your information the information which I printed out at the first stage (before sort) is using bash.

Appreciate your help. Thanks.

> cat file22
1 6 4 8 2 3
2 1 9 3 2 1
3 3 5 6 3 1
4 9 7 8 2 3
> sort -t" " -k2 file22
2 1 9 3 2 1
3 3 5 6 3 1
1 6 4 8 2 3
4 9 7 8 2 3

Above was sorted, with delimiter of space between fields, beginning at the 2nd field.

Hi Joey,

I meant sorting the field 2,field 3 ,field4 and field 5 for each line.

My example of input:-

1 6 4 8 2 3
2 1 9 3 2 1
3 3 5 6 3 1
4 9 7 8 2 3

Output:-
1 2 3 4 6 8
2 1 1 2 3 9
3 1 3 3 5 6
4 2 3 7 8 9

Observe the numbers in field 2 to field 5 are sorted from ascending.

The first position is the row number.
You are then sorting all the members on each row?
That would be positions 2 thru 6?

Hi

The first field is some other numbers and the example happen to be somehow row number.

You are right..it is from field 2 to field 6.

perl:

open FH,"<file";
while(<FH>){
	$_=~tr/\n//d;
	my($fir,@arr)=split;
	print $fir," ",join(" ",sort {$a<=>$b} @arr),"\n";	
}
close FH;

Hi,

Thanks for the suggestion. But I am looking at solution for bash so I could pass the sorted array directly. I need to do some extra computation on the sorted array before printed out to a new file.

> cat file22
1 6 4 8 2 3
2 1 9 3 2 1
3 3 5 6 3 1
4 9 7 8 2 3

> cat sort_22
#! /usr/bin/bash

while read line
  do
  line_a=`echo $line | cut -d" " -f1`
  line_b=`echo $line | cut -d" " -f2- | tr " " "\n" | sort -n | tr "\n" " "`
  echo $line_a $line_b
done <file22

> sort_22
1 2 3 4 6 8
2 1 1 2 3 9
3 1 3 3 5 6
4 2 3 7 8 9
>