Script to make a table from Named Variables.


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Script to make a table from Named Variables.
# 1  
Old 06-02-2011
Data Script to make a table from Named Variables.

I need a shell script to make a table from Named Variables

Input File(Can have multiple lines):
Code:
a=1,b=2,d=4,e=5
a=11,b=12,c=13,d=14

Output file:
Code:
a,b,c,d,e
1,2,,4,5
11,12,13,14,

Thanks in advance

Last edited by Scott; 06-02-2011 at 08:14 AM.. Reason: Code tags
# 2  
Old 06-02-2011
awk -F"[,|\=]" '{print $2,$4,$6,$8}' filename
# 3  
Old 06-02-2011
Parse each file based on delimeter ,
Now print the 1st & 2nd column respectively.

Have a try
Do let us know in case of difficulty.
# 4  
Old 06-02-2011
Pls see that a,b,c,d,e represent the columns.
The 1st line do not contain "c". so in third column of corresponding row I want blank or zero.
The 2nd line do not have "e". so in fifth column of corresponding row I want blank or zero.
Pls suggest.
# 5  
Old 06-02-2011
Give us ample data to test, at least 10-20 lines.
# 6  
Old 06-02-2011
Here I am giving more data to test

Input File:
Code:
a=1,c=3,d=4,e=5,f=6,g=7,h=8
a=11,b=12,d=14,e=15,f=16,g=17,h=18
a=21,b=22,c=23,e=25,f=26,g=27,h=28
a=31,b=32,c=33,d=34,f=36,g=37,h=38
a=41,b=42,c=43,d=44,e=45,g=47,h=48
a=51,b=52,c=53,d=54,e=55,f=56,h=58
a=61,b=62,c=63,d=64,e=65,f=66,g=67,
a=71,b=72,c=73,d=74,e=75,f=76,g=77,h=78
a=81,b=82,c=83,d=84,e=85,f=86,g=87,h=88
a=91,b=92,c=93,d=94,e=95,f=96,g=97,h=98
a=101,b=102,c=103,d=104,e=105,f=106,g=107,h=108
a=111,c=113,d=114,e=115,f=116,g=117,h=118
a=121,b=122,c=123,e=125,f=126,g=127,h=128
a=131,b=132,c=133,d=134,e=135,f=136,g=137,h=138
a=141,b=142,c=143,d=144,e=145,f=146,g=147,h=148
a=151,b=152,c=153,d=154,e=155,g=157,h=158
a=161,b=162,c=163,d=164,e=165,f=166,g=167,h=168
a=171,b=172,c=173,d=174,f=176,g=177,h=178
a=181,b=182,d=184,f=186,g=187,h=188

Output file required (see there are double comma at place of missing field in rows)
Code:
a,b,c,d,e,f,g,h
1,,3,4,5,6,7,8
11,12,,14,15,16,17,18
21,22,23,,25,26,27,28
31,32,33,34,,36,37,38
41,42,43,44,45,,47,48
51,52,53,54,55,56,,58
61,62,63,64,65,66,67,
71,72,73,74,75,76,77,78
81,82,83,84,85,86,87,88
91,92,93,94,95,96,97,98
101,102,103,104,105,106,107,108
111,,113,114,115,116,117,118
121,122,123,,125,126,127,128
131,132,133,134,135,136,137,138
141,142,143,144,145,146,147,148
151,152,153,154,155,,157,158
161,162,163,164,165,166,167,168
171,172,173,174,,176,177,178
181,182,,184,,186,187,188


Last edited by Scott; 06-02-2011 at 08:15 AM.. Reason: Please use code tags
# 7  
Old 06-02-2011
Hope this is what you are looking for
Code:
awk -F, '

{
for(i=1;i<NF;i++)
{
    pos=index($i,"="); 
    ARR[substr($i,0,pos-1),NR] =  substr($i,pos+1,length($i));
    SUB[substr($i,0,pos-1)]
}
max_row=NR;
}
END{
    for (a in SUB){ 
        printf " %s \t",a; }
    printf "\n";

    for(i=1;i<=max_row;i++){

        for (a in SUB){
            if(ARR[a,i] != "")            
                printf " %s \t",ARR[a,i];
            else
                printf " 0 \t";
        }    
        printf "\n"
    }

}' test.txt

Output
Code:
 a       b       c       d       e       f       g
 1       0       3       4       5       6       7
 11      12      0       14      15      16      17
 21      22      23      0       25      26      27
 31      32      33      34      0       36      37
 41      42      43      44      45      0       47
 51      52      53      54      55      56      0
 61      62      63      64      65      66      67
 71      72      73      74      75      76      77
 81      82      83      84      85      86      87
 91      92      93      94      95      96      97
 101     102     103     104     105     106     107
 111     0       113     114     115     116     117
 121     122     123     0       125     126     127
 131     132     133     134     135     136     137
 141     142     143     144     145     146     147
 151     152     153     154     155     0       157
 161     162     163     164     165     166     167
 171     172     173     174     0       176     177
 181     182     0       184     0       186     187
 0       0       0       0       0       0       0

You can change the formatting by chaning the printf statements.

Last edited by kumaran_5555; 06-02-2011 at 05:25 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to make entries background tasks in the table?

Hi am new to unix , when the background task is running how to put entry in the table and also if there is any issue how to stop the running task. can anyone help me... (1 Reply)
Discussion started by: Venkatesh1
1 Replies

2. Shell Programming and Scripting

Make a table from a text file

Hi, I have a pipe separated text file. Can some someone tell me how to convert it to a table? Text File contents. |Activities|Status1|Status2|Status3| ||NA|$io_running2|$io_running3| |Replication Status|NA|$running2|$running3| ||NA|$master2|$master3|... (1 Reply)
Discussion started by: rocky88
1 Replies

3. Shell Programming and Scripting

Find and replace variables using a csv table

I have a flat file (template) where I want to replace variables based upon a value in another file (csv). The variables in the template are named %VAR_X_z% The values are in the csv file and X is field 0 of each line and y field 1 and up. Example of the csv: Badidas, 13.00, 12.00, 11.00,... (8 Replies)
Discussion started by: biscayne
8 Replies

4. UNIX for Advanced & Expert Users

How to make the variables of one script available for the other scripts?

Hello Everyone, I want to know how can we make the variables of one script available for the other script? for example i have three scripts variable_availability.sh,first.sh,second.sh and a file containing variables called common ---------------------------------- cat variable_availability.sh... (2 Replies)
Discussion started by: Kesavan
2 Replies

5. Solaris

how do i make a route entry permanent in the routing table on solaris 8?

how do I make sure that the entry in the routing table on Solaris 8 stay permanent after rebooting the server. For example route add 172.20.1.60 -netmask 255.255.255.0 172.20.255.253 Each time the server reboots the entry disappears when using the command netstat -nr (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies

6. Shell Programming and Scripting

Converting html table data into multiple variables.

Hi, Basically what I am trying to do is the following. I have created a shell script to grab timetabling information from a website using curl then I crop out only the data I need which is a table based on the current date. It leaves me with a file that has the table I want plus a small amount... (2 Replies)
Discussion started by: domsmith
2 Replies

7. UNIX for Dummies Questions & Answers

Trying to make fixtures table with lynx --dump and pipe filters

Hey, I'm trying to make a nice clear table of fixtures. lynx --dump Fixtures & Reports | Fixtures | Arsenal.com | tail -n+360 | less #tail to remove 1st 360 line I'm trying to remove the 'Add to Calendar' bit next I tried pipping through sed but not sure if I did it right sed 's/\Add... (3 Replies)
Discussion started by: 64mb
3 Replies

8. UNIX for Dummies Questions & Answers

MAKE and its macros and variables

I want to build a Makefile that simply takes a template file and modifies it (sed or perl, probably) before installing the result in the right place - my problem is creating the variable for substitution... So I have SYSTEM = SYS1 SYS2 SYS1_CHANNELS = CHANNEL1 CHANNEL2 CHANNEL4... (1 Reply)
Discussion started by: JerryHone
1 Replies

9. Shell Programming and Scripting

How to make variables in script function local?

Is it possible to make function variables local? I mean for example, I have a script variable 'name' and in function I have declared variable 'name' I need to have script's 'name' have the same value as it was before calling the function with the same declaration. The way to preserve a... (5 Replies)
Discussion started by: alex_5161
5 Replies

10. UNIX for Dummies Questions & Answers

Read variables from Access table

Hi! I've just started learning shell scripting, and have been somewhat 'thrown in at the deep-end and told to swim' so excuse my complete lack of knowledge and ignorance, but here goes... I've been given a unix script to 'tidy up'. Basically the script consists of the few lines of code being... (2 Replies)
Discussion started by: Sn33R
2 Replies
Login or Register to Ask a Question