- 论坛徽章:
- 0
|
sed 's/[&=]/ /g' file|awk '{for(k=1;k<=5;k++) a[k]="x";for(k=1;k<=NF;k+=2) a[$k]=$(k+1);for(k=1;k<=5;k++){if(a[k] !~ /[0-9]+/) a[k]=0;} print a[1],a[2],a[3],a[4],a[5];}' |
上面的5是根据一行数据项的数目来定的.
用perl
perl -nWle '@exp=/&(\d+=\d+)/g;@field[0..4]=(0,0,0,0,0);do { s/^(\d+)/\$field[$1-1]/;eval ($_);} for @exp;print join(" ",@field);' file
简化一下:
perl -nWle '$,=" ";@field{1..5}=(0,0,0,0,0);%field=(%field,/\d+/g);print map { $field{$_} } sort keys %field;' file
也写个bash的
cat file|while read line;do result=(0 0 0 0 0 0);field=(${line//[&=]/ });result=$field;for((i=1;i<${#field[@]};i+=2));do pos=${field[$i]};value=${field[$i+1]};result[$pos]=$value;done;echo "${result[@]}";done
[ 本帖最后由 dream3401 于 2009-3-6 07:44 编辑 ] |
|