![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sorting/arrangement problem | Abhishek Ghose | Shell Programming and Scripting | 7 | 05-25-2007 06:19 AM |
| Help on file arrangement | kharen11 | UNIX for Dummies Questions & Answers | 5 | 03-10-2007 10:31 PM |
| Cell arrangement | bobo | UNIX for Dummies Questions & Answers | 12 | 08-21-2006 05:42 PM |
| Data arrangement | bobo | UNIX for Dummies Questions & Answers | 4 | 08-18-2006 07:02 AM |
| Text file arrangement | merry susana | UNIX for Dummies Questions & Answers | 8 | 06-17-2005 01:11 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
For first line you can use the following
Code:
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 Last edited by anbu23; 09-20-2006 at 01:20 PM. |
|
#3
|
|||
|
|||
|
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! |
|
#4
|
||||
|
||||
|
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: Code:
#! /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
Code:
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 Tayyab Last edited by tayyabq8; 09-19-2006 at 11:37 PM. |
|
#5
|
|||
|
|||
|
Thanks I will try!
|
|
#6
|
|||
|
|||
|
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! |
|
#7
|
||||
|
||||
|
Try to put IFS=";" before loop runs, if you find it difficult search these forums for IFS(Internal Field Separator), its ksh builtin.
Last edited by tayyabq8; 09-21-2006 at 07:36 AM. Reason: Spelling |
||||
| Google The UNIX and Linux Forums |