问题
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