Sunday, June 22, 2008

Extract key value using awk in bash


Input file:

$ cat keyval.txt
Geo=Asia
Sub=Awk
Pt=7.8


Output required:


keys=Geo|Sub|Pt val=Asia|Awk|7.8



$ awk -F= 'END { printf "keys=%s val=%s\n", k, v }
{ k = k ? k s $1 : $1; v = v ? v s $2 : $2 }
' s="|" keyval.txt


.

1 comment:

Michael said...

hacky, but it works
rawdata[1]="hey"
rawdata[30]="yo"

j=1;
for val in ${rawdata[@]}; do
key=`echo ${!rawdata[@]} | cut -d" " -f${j}`

echo $key $val

let j=j+1
done





1 hey
2 yo

© Jadu Saikia www.UNIXCL.com