Tony
Tony

Reputation: 9581

Reformat columns/table to key/value lines in Bash

One of my commands in my Bash script outputs some data in table format:

name               requested state   instances   memory   disk   urls
test-1-API   started           1/1         2G       2G     test-1-apidomain.com
test-2-API   started           1/1         2G       2G     test-2-apidomain.com
test-3        started           1/1         3G       1G     test-3domain.com
test-4-API     started           1/1         3G       2G     test-4-apidomain.com

How do I get this data into rows? I.e.,

name=test-1-API requested=started state=1/1 instances=2G

etc...?

Upvotes: 0

Views: 71

Answers (1)

karakfa
karakfa

Reputation: 67507

awk to the rescue!

$ awk -v ORS='\n\n' 'NR==1 {split($0,h); next} 
                           {for(i=1;i<=NF;i++) $i=h[i]"="$i}1' file

name=test-1-API requested=started state=1/1 instances=2G memory=2G disk=test-1-apidomain.com

name=test-2-API requested=started state=1/1 instances=2G memory=2G disk=test-2-apidomain.com

name=test-3 requested=started state=1/1 instances=3G memory=1G disk=test-3domain.com

name=test-4-API requested=started state=1/1 instances=3G memory=2G disk=test-4-apidomain.com

Upvotes: 1

Related Questions