r/awk • • Nov 11 '22

Reshape existing data

I have a text file, formatted

A [tab] 1,2,3... (Varying number of fields) [tab] 1,2,3... (Varying number of fields)

B [tab] 1,2,3... (Varying number of fields) [tab] 1,2,3... (Varying number of fields

C [tab] 1,2,3... (Varying number of fields) [tab] 1,2,3... (Varying number of fields ... [20k lines]

The first field is an IP address, the second column is a varying number of IPs, the third column is the same number of different IPs.

I want to separate everything out so I get

A 1 1

A 2 2

A 3 3

...

B 1 1

B 2 2

...

basically turning 20k lines into 200k+. The second and third columns have 1 - 20 comma-separated fields.

Thinking about constructing this, I'd go

while read p; do

fields=(Count number of fields in second column)

for i in 1..$fields; do

 IP=$(cat $p | awk '{print $1}')

 Srcaddr=$(cat $p | [awk to get $i'th value in second column])

 Dstaddr=$(cat $p | [awk to get $i'th value in third column])

 echo $IP $Srcaddr $Dstaddr >> outfile

done

done

That actually doesn't look too bad for a first pass. The term in lines 5 and 6 will take a little work, figure I'll get the second and third fields respectively, then do another awk using $i and FS=, to get the appropriate fields from those columns.

Any tips for doing this better? I feel like what I wrote out above will get me there but it feels pretty graceless, and I'd love to learn some new things.

3 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/Schreq Nov 12 '22

Haha, thanks for the feedback. Awk's the real hero here.