如何将多个字段分配给一个变量?

发布于 2024-12-10 09:51:02 字数 309 浏览 0 评论 0原文

我想使用 awk 1liner

cat data.txt

将 data.txt 文件转换为以下输出
  • personA neta netb netc
  • personB meta metb metc metd

....

输出:

  • personA 的包中有 {neta netb netc} 物品
  • personB 的包中有 {meta metb metc metd} 物品

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:

  • personA has {neta netb netc} items in his bag
  • personB has {meta metb metc metd} items in his bag

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Smile简单爱 2024-12-17 09:51:02

这是 subtr() 和 index() 的一个很好的用途。

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

输出:

personA 的包里有 {neta netb netc} 项。

personB 的包里有 {meta metb metc metd} 件物品。

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.

柳若烟 2024-12-17 09:51:02
awk '{$1=$1" has";$2="{"$2;print $0"} items in his bag"}' data.txt
awk '{$1=$1" has";$2="{"$2;print $0"} items in his bag"}' data.txt
⒈起吃苦の倖褔 2024-12-17 09:51:02
while read line; do
  set -- $line
  person=$1
  shift
  printf "%s has {$s} items in his bag" "$person" "$*"
done < data.txt
while read line; do
  set -- $line
  person=$1
  shift
  printf "%s has {$s} items in his bag" "$person" "$*"
done < data.txt
青丝拂面 2024-12-17 09:51:02

怎么样:

awk '{ items=""; 
       for(i=2;i<=NF;i++) {items=items" "$i}; 
       gsub(/^[ ]/, "", items);
       print $1" has {"items"} items in his bag" }' 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文