AWK打印带有变量的字符串 [英] Awk print string with variables

查看:649
本文介绍了AWK打印带有变量的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打印带有变量的字符串?

How do I print a string with variables?

尝试

awk -F ',' '{printf /p/${3}_abc/xyz/${5}_abc_def/}' file

在输出端需要它

/p/APPLE_abc/xyz/MANGO_abc_def/

其中${3} = APPLE${5} = MANGO

推荐答案

printf允许对变量进行插值.以此作为测试文件:

printf allows interpolation of variables. With this as the test file:

$ cat file
a,b,APPLE,d,MANGO,f

我们可以使用printf来实现所需的输出,如下所示:

We can use printf to achieve the output you want as follows:

$ awk -F, '{printf "/p/%s_abc/xyz/%s_abc_def/\n",$3,$5;}' file
/p/APPLE_abc/xyz/MANGO_abc_def/

printf中,字符串%s表示在此处作为字符串插入变量.我们出现了两次%s,一次是$3,一次是$5.

In printf, the string %s means insert-a-variable-here-as-a-string. We have two occurrences of %s, one for $3 and one for $5.

这篇关于AWK打印带有变量的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆