gafm
gafm

Reputation: 351

How to Insert 0 (zero) starting on the 2nd row of output

I have a small data that is structured like this

File1

vol1 10GB 123434
vol2 55GB 231554
vol3 20GB 989765
vol4 17GB 099312

File2

vol4 server1 099312
vol4 server2 099312
vol4 server3 099312
vol4 server4 099312

and the idea is to output data like the following. Assigned the size to a variable and insert it to the 1st row and then the rest should be zero.

size=`cat file1 |grep 099312 | awk '{print $2}'`


vol4 server1 17GB 099312
vol4 server2  0   099312
vol4 server3  0   099312
vol4 server4  0   099312

I'm not really sure if that's the right approach but right now I'm kinda lost.

BTW, file 2 is dynamic and it could be more than 4 rows but the same objective.. size should only be applied/inserted on the first row.

Upvotes: 0

Views: 59

Answers (2)

karakfa
karakfa

Reputation: 67507

another awk

$ awk 'NR==FNR {a[$1]=$2; next} 
               {print $1,$2,a[$1],$3; 
                a[$1]=0}' file{1,2} | column -t

vol4  server1  17GB  099312
vol4  server2  0     099312
vol4  server3  0     099312
vol4  server4  0     099312

centering "0" will require additional formatting, not sure it was required.

Upvotes: 2

Shawn
Shawn

Reputation: 52409

Try:

awk 'NR==FNR { vols[$1]=$2; next }
     { if ($1 in vols) {
         print $1, $2, vols[$1], $3
         delete vols[$1]
       } else {
         print $1, $2, "0", $3
       }
     }' file1 file2

Upvotes: 1

Related Questions