开发者

how to assign multiple field into a variable?

i would like to convert the data.txt file into the following output using awk 1liner

cat data.txt

  • personA neta netb netc
  • personB meta metb metc metd

....

output:


this is a good use for subtr() and index().

awk '{print $1FS"has {"substr($0,index($0,$2))"} itmes in his bag."}' data.txt

output:

personA has {neta netb netc} itmes in his bag.

personB has {meta metb metc metd} itmes in his bag.


awk '{$1=$1" has";$2="{"$2;print $0"} items in his bag"}' data.txt


while read line; do
  set -- $line
  person=$1
  shift
  printf "%s has {$s} items in his bag" "$person" "$*"
done < data.txt


What about:

awk '{ items=""; 
       for(i=2;i<=NF;i++) {items=items" "$i}; 
       gsub(/^[ ]/, "", items);
       print $1" has {"items"} items in his bag" }' data.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜