Data arrangement


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Data arrangement
# 1  
Old 09-19-2006
Data 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!
# 2  
Old 09-19-2006
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 05:20 PM..
# 3  
Old 09-20-2006
Data

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  
Old 09-20-2006
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-20-2006 at 03:37 AM..
# 5  
Old 09-20-2006
Thanks I will try!
# 6  
Old 09-21-2006
Data

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  
Old 09-21-2006
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 11:36 AM.. Reason: Spelling
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Retreive data with arrangement

Hi all I have following part of a big file TTDS00002 Synonyms M1 receptor TTDS00002 Disease Alzheimer's disease TTDS00002 Disease Bronchospasm (histamine induced) TTDS00002 Disease Cognitive deficits TTDS00002 Disease Schizophrenia TTDS00002 Function The muscarinic acetylcholine... (2 Replies)
Discussion started by: kareena
2 Replies

2. UNIX for Advanced & Expert Users

Data re-arrangement

Hi I have a huge problem to solve ASAP. Can someone please help!!! My format is arranged in this format: It has three columns. LOGIN ALIAS REC_ID A BB1 0 A ... (1 Reply)
Discussion started by: Mapilo
1 Replies

3. Shell Programming and Scripting

Help with data re-arrangement problem facing

Input file: <symbol>Q9Y8G1</symbol> <name>Q9Y8G1_EMENI</name> <symbol>Q6V953</symbol> <symbol>Q5B8K1</symbol> <name>Q6V953_EMENI</name> <symbol>G1A416</symbol> <name>G1A416_9FUNG</name> <symbol>D4N894</symbol> <name>D4N894_PLEER</name> <symbol>B0FFU4</symbol>... (13 Replies)
Discussion started by: cpp_beginner
13 Replies

4. Shell Programming and Scripting

Help to data re-arrangement problem

Input file <data id>="1">\ </data>\ <data id>="2">\ </data>\ <code>="1" target="2">\ </code>\ <data id>="1">\ </data>\ <data id>="2">\ </data>\ <code>="1" target="2">\ </code>\ <data id>="1">\ </data>\ <data id>="2">\ </data>\ <code>="1" target="2">\ </code>\ (2 Replies)
Discussion started by: cpp_beginner
2 Replies

5. Shell Programming and Scripting

Re-arrangement of data

Dear Frineds, I have a flat file as follows ABCD ABDCWQE POIERAS ADSGASGFG GHJKFHD XBDFGHFGDH POIU IJPFG AFGJFPGOU A;DGUPGU SFSDFDSDFHDSF SDFGHSFDH I want this column to be converted into row like follows ABCD, ABDCWQE, POIERAS, ADSGASGFG, GHJKFHD, XBDFGHFGDH (6 Replies)
Discussion started by: anushree.a
6 Replies

6. Shell Programming and Scripting

sorting/arrangement problem

Hi, I have the following 'sorting' problem. Given the input file: a:b:c:12:x:k s:m:d:8:z:m a:b:c:1:x:k p:q:r:23:y:m a:b:c:3:x:k p:q:r:1:y:m the output I expect is: a:b:c:1:x:k p:q:r:1:y:m s:m:d:8:z:m What happened here is I grouped together lines having the same values for... (7 Replies)
Discussion started by: Abhishek Ghose
7 Replies

7. UNIX for Dummies Questions & Answers

Help on file arrangement

Can anyone help me on this. I have a file that looks like this: color red green blue color pink yellow number one two gender male gender female The output would look like this: color red green blue pink yellow number one two gender male female I have over 5000 rows and i dont want... (5 Replies)
Discussion started by: kharen11
5 Replies

8. UNIX for Dummies Questions & Answers

Cell arrangement

I have a data file with hundreds of lines: I want to place a YES right below the line that say mydata....can someone please help! on the left is my the original data on the right the data format need to be: left > Right mydata > mydata yes > yesno > no mydata > mydata... (12 Replies)
Discussion started by: bobo
12 Replies

9. UNIX for Dummies Questions & Answers

Data arrangement

I have these following data: Home Tom Member List 100 Yes 200 No Home Tom Member List 1 No 2 Yes Home Tome Member List 3 No 400 Yes I want my data to be consistants like this: (4 Replies)
Discussion started by: bobo
4 Replies

10. UNIX for Dummies Questions & Answers

Text file arrangement

Dear UNIX experts: Hi, I have a text file which the contents are arranged vertically down, line by line. How do use a loop (I think) to make it arrange in vertical arrangement with a tab delimitated and write to a new file? Eg: of source file Hello World Good-day Thanks Welcome The... (8 Replies)
Discussion started by: merry susana
8 Replies
Login or Register to Ask a Question