The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-19-2006
Registered User
 

Join Date: Jan 2006
Posts: 145
Unhappy 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!
Reply With Quote
Forum Sponsor
  #2  
Old 09-19-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
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
for second data arrangement you can use the above way

Last edited by anbu23; 09-20-2006 at 01:20 PM.
Reply With Quote
  #3  
Old 09-19-2006
Registered User
 

Join Date: Jan 2006
Posts: 145
Unhappy

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!
Reply With Quote
  #4  
Old 09-19-2006
tayyabq8's Avatar
Moderator
 

Join Date: Nov 2004
Location: Bahrain
Posts: 555
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
Which will yeild your desired output:
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
Regards,
Tayyab

Last edited by tayyabq8; 09-19-2006 at 11:37 PM.
Reply With Quote
  #5  
Old 09-20-2006
Registered User
 

Join Date: Jan 2006
Posts: 145
Thanks I will try!
Reply With Quote
  #6  
Old 09-21-2006
Registered User
 

Join Date: Jan 2006
Posts: 145
Unhappy

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!
Reply With Quote
  #7  
Old 09-21-2006
tayyabq8's Avatar
Moderator
 

Join Date: Nov 2004
Location: Bahrain
Posts: 555
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0