在Bash中将列/表重新格式化为键/值行

时间:2018-10-30 01:43:03

标签: bash awk

我的Bash脚本中的命令之一以表格式输出一些数据:

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

如何将这些数据分成行?即

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

等等...?

1 个答案:

答案 0 :(得分:1)

awk来营救!

$ 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