Extract values from specific row, column using bash

安稳与你 提交于 2021-01-28 15:05:28

问题


I have a sample log file with fields and values in this format

value1 value2
 50    100

value3 value4 
 10      150   

value5
 200

I need to extract fields and values to something of this format

value1=50
value2=100
value3=10
value4=150
value5=200

回答1:


Try this awk:

gawk '{split($0,n_arr," "); getline; n=split($0,v_arr," "); getline; for (i=1;i<=n;i++){print n_arr[i] "=" v_arr[i]}}'



回答2:


awk '/value/ {
                 if ((getline values) > 0) {
                     split(values, array)
                     for (i = 1; i <= NF; i++)
                         print $i "=" array[i]
                 }
              }' inputfile

Instead of /value/ you might use /[[:alpha:]]/




回答3:


awk '
    NF==0 {next}
    var[1] == "" {for (i=1; i<=NF; i++) var[i]=$i; next}
    {for (i=1; i<=NF; i++) {print var[i] "=" $i; delete var[i]}}
'



回答4:


This might work for you:

sed '/^$/d;N;:a;s/\([^ \n]*\) *\([^\n]* *\n\) *\([^ \n]*\) *\(.*\)/\2\4\n\1=\3/;/^\n\n/!ba;s///' file


来源:https://stackoverflow.com/questions/10501030/extract-values-from-specific-row-column-using-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!