How to convert 2 column data into multiple columns based on a keyword in a row??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert 2 column data into multiple columns based on a keyword in a row??
# 1  
Old 02-16-2010
How to convert 2 column data into multiple columns based on a keyword in a row??

Hi Friends

I have the following input data in 2 columns.
Code:
SNo   1
I1  Value
I2  Value
I3 Value
SNo   2
I4  Value
I5  Value
I6  Value
I7  Value
SNo  3
I8  Value
I9  Value
...............
................
SNo N

I want the following output(Multi column)

Code:
SNo     1       SNo   2      SNo   3        ........SNo  N
I1      Value   I4    Value  I8    Value            Ix   Value
I2      Value   I5    Value  I9    Value            Iy   Value
I3      Value   I6    Value
                I7    Value

Ultimately I need a multi column output from 2 column input and needs to be segmented at the specified keyword. i.e. in this case SNo

I hope my problem statement is very clear.

Thanks in advance...

---------- Post updated at 11:49 AM ---------- Previous update was at 11:47 AM ----------

Small correction in the output. I7 Value should be below I6 Value. Because of formatting problem while posting it happened like that.

Last edited by zaxxon; 02-16-2010 at 02:29 AM.. Reason: use code tags please, ty
# 2  
Old 02-16-2010
try next script

Code:
#!/usr/bin/ksh
N=3
count=1
filenames=" "
while [[ $count -lt  $N+1  ]];do
      awk 'BEGIN {RS="SNo"} /No[[:space:]]*'$count'/ {print $0} ' infile > /tmp/tmpf$count
      filenames=$filenames" /tmp/tmpf$count"
   (( count += 1 ))
done

paste $filenames

I edited th post to rewrite the code to replace the paste command with awk command to get best output from the old script:
Code:
#!/usr/bin/ksh
N=3
count=1
filenames=" "
while [[ $count -lt  $N+1  ]];do
      awk 'BEGIN {RS="SNo"} /No[[:space:]]*'$count'/ {print $0} ' infile >  /tmp/tmpf$count
   (( count += 1 ))
done

count=2
while [[ $count -lt  $N+1  ]];do
   awk 'NR==FNR { a[c=FNR]=$0; next } { printf "%-18s\t%-18s\n", a[FNR], $0 } END { for(i=FNR+1;i<=c;i++) print a[i] }' /tmp/tmpf1  /tmp/tmpf$count > /tmp/tfo
   cat /tmp/tfo > /tmp/tmpf1
   (( count += 1 ))
done
cat /tmp/tmpf1



# ./s1
No   1                   No   2                  No  3
I1  Value               I4  Value               I8  Value
I2  Value               I5  Value               I9  Value
I3  Value               I6  Value
                           I7  Value


Last edited by ironmask2004; 02-16-2010 at 07:41 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join columns across multiple lines in a Text based on common column using BASH

Hello, I have a file with 2 columns ( tableName , ColumnName) delimited by a Pipe like below . File is sorted by ColumnName. Table1|Column1 Table2|Column1 Table5|Column1 Table3|Column2 Table2|Column2 Table4|Column3 Table2|Column3 Table2|Column4 Table5|Column4 Table2|Column5 From... (6 Replies)
Discussion started by: nv186000
6 Replies

2. Shell Programming and Scripting

Paste columns based on common column: multiple files

Hi all, I've multiple files. In this case 5. Space separated columns. Each file has 12 columns. Each file has 300-400K lines. I want to get the output such that if a value in column 2 is present in all the files then get all the columns of that value and print it side by side. Desired output... (15 Replies)
Discussion started by: genome
15 Replies

3. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

4. Shell Programming and Scripting

Convert Data from Column to Row

Hi FileA.txt E_TIM 16, ETE 15, EOND 26, EEC 81, E_1 un, E_2 un, E_3 un, E_4 284, E_TIM 17, ETE 15, EOND 29, EEC 82, E_1 un, E_2 un, E_3 un, E_4 249, (6 Replies)
Discussion started by: asavaliya
6 Replies

5. Shell Programming and Scripting

Convert row to columns start from nth column

Dear All, We have input like this: 161 57 1378 176 1392 262 1444 441 1548 538 1611 670 1684 241 57 1378 208 1393 269 1447 444 1549 538 1610 677 1700 321 ... (4 Replies)
Discussion started by: attila
4 Replies

6. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

7. Shell Programming and Scripting

Splitting data from one row as multiple columns

Hi I have a file containing some data as follows: 11-17-2010:13:26 64 4 516414 1392258 11-17-2010:13:26 128 4 586868 695603 11-17-2010:13:26 256 4 474937 1642294 11-17-2010:13:32 64 4 378715 1357066 11-17-2010:13:32 128 4 597981 1684006 ... (17 Replies)
Discussion started by: annazpereira
17 Replies

8. Shell Programming and Scripting

sum multiple columns based on column value

i have a file - it will be in sorted order on column 1 abc 0 1 abc 2 3 abc 3 5 def 1 7 def 0 1 -------- i'd like (awk maybe?) to get the results (any ideas)??? abc 5 9 def 1 8 (2 Replies)
Discussion started by: jjoe
2 Replies

9. Shell Programming and Scripting

Convert row data to column data

Hi Guys, I have a file as follows: a 1 b 786 c 90709 d 99 a 9875 b 989 c 887 d 111 I want: a 1 9875 b 786 989 (3 Replies)
Discussion started by: npatwardhan
3 Replies

10. Shell Programming and Scripting

Convert two column data into 8 columns

Apologies if this has been covered - I did search but couldn't find what I was looking for. I have a simple X-Y input file. I want to convert it from two columns into 8 columns - 4 pairs of X-Y data. So my input file looks like X1 Y1 X2 Y2 X3 Y3 X4 Y4 X5 Y5 etc And I want it to look... (8 Replies)
Discussion started by: NickC
8 Replies
Login or Register to Ask a Question