我想使用awk 1liner
将data.txt文件转换为以下输出cat data.txt
- personA neta netb netc
- personB meta metb metc metd
...
输出:
答案 0 :(得分:2)
这对subtr()和index()很有用。
awk '{print $1FS"has {"substr($0,index($0,$2))"} itmes in his bag."}' data.txt
personA在他的包里有{neta netb netc} itmes。
personB在他的包里有{meta metb metc metd} itmes。
答案 1 :(得分:1)
awk '{$1=$1" has";$2="{"$2;print $0"} items in his bag"}' data.txt
答案 2 :(得分:1)
while read line; do
set -- $line
person=$1
shift
printf "%s has {$s} items in his bag" "$person" "$*"
done < data.txt
答案 3 :(得分:0)
怎么样:
awk '{ items="";
for(i=2;i<=NF;i++) {items=items" "$i};
gsub(/^[ ]/, "", items);
print $1" has {"items"} items in his bag" }' data.txt