Data arrangement

10 2 1 2 3 4 5 6 7 8

20 3 2 1 3 2 9 8 2 1

Need the data to be arranged:

10 2 1 5
2 6
3 7
4 8

20 3 2 1
1 2
3 8
2 9

please help!

For first line you can use the following

while read a b c d e f g h i j  
do
echo $a $b $c $g
echo $d $h
echo $e $i
echo $f $j
done <file

for second data arrangement you can use the above way

My datafile has about 50 lines how can go about to create a while loop to read datafile and diplace them as shown?

Thank you much!

First of all it sounds like a homework.

Secondly your input is not clear, you should post at least 5 lines of your input file and their required output to get the exact logic of what you want, as per your given 2 lines what I understand is that, if line no. is odd then take frist 3 columns, leave next 3 columns and print a single column then print one incremented column (4th col ) and print next column after leaving 3 columns, if line no. is even then reverse the order, take first 3 cols and last column, then take the 4th column and 2nd last, then 5th and 3rd last and so on.

Here is the code as per above logic:

#! /bin/ksh
cnt=1
while read a b c d e f g h i j; do
if [[ $cnt%2 -eq 1 ]]; then
        echo $a $b $c $g
        echo $d $h
        echo $e $i
        echo $f $j
else
        echo $a $b $c $j
        echo $d $i
        echo $e $h
        echo $f $g
fi
(( cnt=cnt+1 ))
done < inputfile

Which will yeild your desired output:

admin@unix1:/home/admin$./test.sh
10 2 1 5
2 6
3 7
4 8
20 3 2 1
1 2
3 8
2 9

Regards,
Tayyab

Thanks I will try!

It works great! thank you much! By the way it is not homework problem. I learn Unix from book and this wesite. I am great thanked every who had help me with all my asks!!!

Regarding this data arrangement post, I have other questions:

This is my datafile:

33 ;1 ;2 ;2 ;1 ;3 ;3 ;1 ;3 ; ; ; ; ;2 ;1 ;1 ;2 ; ; ;2 ; ; ;
111;1 ;2 ;2 ;1 ;3 ;3 ;3 ;3 ; ; ; ; ;2 ;1 ;1 ;2 ;4 ;4 ;4 ;4 ; ;

it is seperated by a ";" .

The code:

#! /bin/ksh
cnt=1
while read a b c d e f g h i j k l m n o p q r s t u v w; do
if [[ $cnt%2 -eq 1 ]]; then
echo $a
echo $b $n
echo $c $o
echo $d $p
echo $e $q
echo $f $r
echo $g $s
echo $h $t
echo $i $u
echo $j $v
echo $k $w
echo $l
echo $m
else
echo $a
echo $b $n
echo $c $o
echo $d $p
echo $e $q
echo $f $r
echo $g $s
echo $h $t
echo $i $u
echo $j $v
echo $k $w
echo $l
echo $m
fi
(( cnt=cnt+1 ))
done < datafile

The resuslts:

33
;1 ;2
;2 ;1
;2 ;1
;1 ;2
;3 ;4
;3 ;4
;1 ;4
;3 ;4
; ;
; ;
;
;
111;1
;2 ;1
;2 ;1
;1 ;2
;3 ;4
;3 ;4
;3 ;4
;3 ;4
; ;
; ;
;
;
;2

The data result are not correct since it printed the 350;1 as a single column.

I want:

33
;1 ;2
;2 ;1
;2 ;1
;1 ;2
;3 ;
;3 ;
;1 ;2
;3 ;
; ;
; ;
;
;

111
;1
;2 ;1
;2 ;1
;1 ;2
;3 ;4
;3 ;4
;3 ;4
;3 ;4
;;
;;

Please help!

Try to put IFS=";" before loop runs, if you find it difficult search these forums for IFS(Internal Field Separator), its ksh builtin.