Put repeated values into a table


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Put repeated values into a table
# 1  
Old 11-25-2013
Put repeated values into a table

Hi all,

I have blocks of records like the following, each block ends in = in a new line, I want tabularize the entire output. The pattern is the same in every block although not all types are there in every block.
For example gine3 is absent in the second block but present first and third.


Code:
line1=ABC
pine2=XYZ
gine3=123 
=
line1=TYU
pine2=UYT
=
line1=BCD
pine2=GHY
gine3=786
tine4=RTY

output
Code:
line1 pine2 gine3 tine4
ABC XYZ 123 
TYU UYT 
BCD GHY 786 RTY


Last edited by ritakadm; 11-25-2013 at 01:08 PM..
# 2  
Old 11-25-2013
Try:
Code:
awk -F"=" 'BEGIN{OFS=" ";print "line1 pine2 gine3 tine4"}$0=="="{print x;x=""}{x=(x?x" ":"")$2}END{print x}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 11-25-2013
Thank you, but my columns are not restricted to only 4, there may be 20 or more, can we make the header variable? should be the maximum number of lines in all blocks
# 4  
Old 11-25-2013
For given input this also will work

Code:
$ awk '!A[$1]++{s = s ? s OFS $1 : $1}{B[++i]=$2}END{print s; for(j=1;j<=i;j++)printf B[j]"%s", B[j] !~ /[[:alnum:]]/ || j == i ? RS : OFS }' FS="=" file

--edit--
Code:
$ awk '!A[$1]++{s = s ? s OFS $1 : $1}{p = !/^\=/ ? p ? p OFS $2 : $2 : p RS }END{gsub(/\n[[:space:]]/,"\n",p);print s RS p}' FS="=" file

Code:
line1 pine2 gine3  tine4
ABC XYZ 123  
TYU UYT 
BCD GHY 786 RTY


Last edited by Akshay Hegde; 11-25-2013 at 03:05 PM.. Reason: some more code
This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 11-25-2013
Hello Akshay,

Thanks for great code could you please explain the same.


Thanks,
R. Singh
# 6  
Old 11-25-2013
Quote:
Originally Posted by RavinderSingh13
Hello Akshay,

Thanks for great code could you please explain the same.


Thanks,
R. Singh
awk '!A[$1]++{s = s ? s OFS $1 : $1}.... --> Here unique records from column 1 are stored in variable s

{B[++i]=$2} --> Array B holds column2 records

and finally in END block

END{print s;.. --> Prints unique records in variable s

for(j=1;j<=i;j++) --> Looping for 1 to i

B[j] !~ /[[:alnum:]]/ || j == i ? RS : OFS --> if B[j] is not alphanumeric or j equal to i thats end of loop print B[j] and Row separator ("\n") else print B[j] and output field separator (OFS)
This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 11-25-2013
Try this on a larger file and come back with results:
Code:
awk -F= 'BEGIN          {LnCnt=0; HdCnt=0}
         !$1 && !$2     {LnCnt++; next}
                        {for (i=0; i<HdCnt; i++) if (HD[i]==$1) break
                         if (i==HdCnt) HD[HdCnt++]=$1
                         A[LnCnt,$1] = $2}

         END            {for (i=0; i<HdCnt; i++) printf "%s\t", HD[i]; printf "\n"
                         for (j=0; j<=LnCnt; j++)
                           {for (i=0; i<HdCnt; i++) printf "%s\t", A[j,HD[i]]; printf "\n"}
                        }
        ' OFS="\t" file
line1    pine2    gine3    tine4    
ABC    XYZ    123         
TYU    UYT            
BCD    GHY    786    RTY

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding the values of repeated ids

File1 consist two columns, first some weired ids and second the numbers t|v203.1@t|k88711.1 0.1 t|v190.1@t|k90369.1 0.01 t|v203.1@t|k88711.1 0.5 t|v322.1@t|k88711.1 0.2 t|v207.1@t|k90369.1 0.11 t|v326.1@t|k85939.1 0.5 t|v207.1@t|k90369.1 0.7 t|v207.1@t|k90369.1 0.3 t|v326.1@t|k89421.1 0.33... (3 Replies)
Discussion started by: ashmit99
3 Replies

2. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

3. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

4. Shell Programming and Scripting

Resume and count repeated values

Gents, Please can you help me. Input file 1050 , 9 ,9888 1050 ,10 ,9888 1050 ,11 ,9888 1050 ,13 ,9888 1050 ,15 ,9888 1051 , 9 ,9889 1051 ,12 ,9889 1051 ,15 ,9889 1051 ,18 ,9889 1052 , 9 ... (7 Replies)
Discussion started by: jiam912
7 Replies

5. Shell Programming and Scripting

How do I put data piped to my script into an array for repeated processing

Hi folks, Sorry for something I'm sure was answered already, I just could not find it in a search of the forums. I am trying to build a script that eats a config file as: cat file.cnf | ConProcess.shWhat I want to do inside the script is: !#/usr/bin/bash # captured piped cat into an... (6 Replies)
Discussion started by: Marc G
6 Replies

6. Shell Programming and Scripting

remove brackets and put it in a column and remove repeated entry

Hi all, I want to remove the remove bracket sign ( ) and put in the separate column I also want to remove the repeated entry like in first row in below input (PA156) is repeated ESR1 (PA156) leflunomide (PA450192) (PA156) leflunomide (PA450192) CHST3 (PA26503) docetaxel... (2 Replies)
Discussion started by: manigrover
2 Replies

7. Shell Programming and Scripting

select values from db1 table and insert into table of DB2

Hi I am having three oracle databases running in three different machine. their ip address is different. from one of the DB am able to access both the databases.(means am able to select values and insert values in to tables individually.) I need to fetch some data from DB1 table(say DB1 ip is... (2 Replies)
Discussion started by: aemunathan
2 Replies

8. Shell Programming and Scripting

comparing the values of repeated keys in multiple columns

Hi Guyz The 1st column of the input file has repeated keys like x,y and z. The ist task is if the 1st column has unique key (say x) and then need to consider 4th column, if it is + symbol then subtract 2nd column value with 3rd column value (we will get 2(10-8)) or if it is - symbol subtract 3rd... (3 Replies)
Discussion started by: repinementer
3 Replies

9. Shell Programming and Scripting

To Delete the repeated occurances and print in same line by appending values

Hi All, I am trying to work on below text a b c 1 a b c 2 a b c 3 x y z 6 x y z 44 a b c 89 Need to delete the occurances and get in single line like below: a b c 1 2 3 89 x y z 6 44 89 Please help me i am new into unix scripting ..... ---------- Post updated at 03:00... (8 Replies)
Discussion started by: shaliniyadav
8 Replies

10. Solaris

Can I put my own tags in a partition table

I have X4500 running Solaris 10. I have formatted a disk and created partition table as given below. Specify disk (enter its number): 0 selecting c0t0d0 /dev/dsk/c0t0d0s0 is part of active ZFS pool zpool1. Please see zpool(1M). FORMAT MENU: disk - select a disk ... (3 Replies)
Discussion started by: bharu_sri
3 Replies
Login or Register to Ask a Question