I am facing a problem in extracting from a spool file!!
The spoolfile is as such eg:
con_prov_id country_name content_id
1643 Australia 1000
1643 Japan 2000
1753 Australia 3000
1753 US 4000
Now I want a out put as in a different file as
con_prov_id country_name content_id
1643 Australia,Japan 1000,2000
1753 Australia,US 3000,4000
Inputs to this will be really helpful plz
Hitori
August 11, 2006, 2:41am
2
#!/bin/awk -f
BEGIN {
cout[""] = 0;
nout[""] = 0;
}
/^[^0-9]/ {
print;
}
/^[0-9]+/ {
cout[$1] = cout[$1] "," $2;
nout[$1] = nout[$1] "," $3;
}
END {
for (i in cout) {
if (i != "") {
print i, substr(cout,2), substr(nout,2);
}
}
}
hitori:
#!/bin/awk -f
BEGIN {
cout[""] = 0;
nout[""] = 0;
}
/^[^0-9]/ {
print;
}
/^[0-9]+/ {
cout[$1] = cout[$1] "," $2;
nout[$1] = nout[$1] "," $3;
}
END {
for (i in cout) {
if (i != "") {
print i, substr(cout,2), substr(nout,2);
}
}
}
Thanks a lot Hitori!!! I am trying if it works fine !!! Thanks again
You may also want to consider join.
See man join
Hitori , Canyou please explain me the
/^[^0-9]/ {
print;
}
/^[0-9]+/ {
cout[$1] = cout[$1] "," $2;
nout[$1] = nout[$1] "," $3;
Can you please tell me what this above code will do ?? I am eager to know this
Hitori
August 28, 2006, 2:13am
6
Sorry for a long reply.
/^[^0-9]/ {
print;
}
means to print all lines that do not begin with a number (i.e. con_prov_id country_name content_id)
/^[0-9]+/ {
cout[$1] = cout[$1] "," $2;
nout[$1] = nout[$1] "," $3;
}
-put all in associative arrays for lines that begin with a number. $1, $2, $3 are the 1st, 2nd and 3rd columns respectively. For instance for line 1643 Australia 1000 we let count[1643]=count[1643] "," "Australia", nout[1643]=nout[1643] "," 1000
END {
for (i in cout) {
if (i != "") {
print i, substr(cout,2), substr(nout,2);
}
}
}
at the END all gathered info are being printed
Hitori , I appriciate your Input.
thanks