Dear All,
I have to find a way to reorganize a table file according to the last column. The input file looks like this:
cat Input1.txt:
ID:12:23:00Q EU232 2342 234 123 231 aa1;ab2
ID:11:22:00E EU112 1232 211 112 233 ab2;ac3
ID:19:24:00S EU121 569 100 101 244 aa1;ac3
ID:11:33:00S EU456 332 120 99 221 ac3
My output file should contain the information of the last column in newly created columns like this:
cat Output:
ID:12:23:00Q EU232 2342 234 123 231 aa1 ab2 na aa1;ab2
ID:11:22:00E EU112 1232 211 112 233 na ab2 ac3 ab2;ac3
ID:19:24:00S EU121 569 100 101 244 aa1 na ac3 aa1;ac3
ID:11:33:00S EU456 332 120 99 221 na na ac3 ac3
My solution:
In a first step I introduced three new columns containing "na" values.
awk '{ print $1,$2,$3,$4,$5,$6,"na","na","na",$7}' input1.txt > input2.txt
This resulted in the following output:
cat Input2.txt
ID:12:23:00Q EU232 2342 234 123 231 na na na aa1;ab2
ID:11:22:00E EU112 1232 211 112 233 na na na ab2;ac3
ID:19:24:00S EU121 569 100 101 244 na na na aa1;ac3
ID:11:33:00S EU456 332 120 99 221 na na na ac3
Now, I replaced the "na" if present in the last column.
awk '/aa1/{gsub($7, "aa1")};{print}' input2.txt > output_aa1.txt
awk '/ab2/{gsub($8, "ab2")};{print}' output_aa1.txt > output_aa1_ab2.txt
awk '/ac3/{gsub($9, "ac3")};{print}' output_aa1_ab2.txt >
This kind of works but is only feasible for a limited number of items (in my example three). Is there a way to upscale it? Something along the lines:
for ITEM in `cat item.list`
do
???
done
Thanks for your help!