I need a shell script to make a table from Named Variables
Input File(Can have multiple lines):
a=1,b=2,d=4,e=5
a=11,b=12,c=13,d=14
Output file:
a,b,c,d,e
1,2,,4,5
11,12,13,14,
Thanks in advance
I need a shell script to make a table from Named Variables
Input File(Can have multiple lines):
a=1,b=2,d=4,e=5
a=11,b=12,c=13,d=14
Output file:
a,b,c,d,e
1,2,,4,5
11,12,13,14,
Thanks in advance
awk -F"[,|\=]" '{print $2,$4,$6,$8}' filename
Parse each file based on delimeter ,
Now print the 1st & 2nd column respectively.
Have a try
Do let us know in case of difficulty.
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.
Give us ample data to test, at least 10-20 lines.
Here I am giving more data to test
Input File:
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)
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
Hope this is what you are looking for
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
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.
Thanks a lot Kumaran_5555
It worked like a charm