awk 怎么引用shell 变量

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
yanyeyy
帖子: 26
注册时间: 2008-10-06 23:04

awk 怎么引用shell 变量

#1

帖子 yanyeyy » 2012-10-22 18:32

awk '/pattern/ {action}' input-file
pattern处怎么引用shell变量?
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: awk 怎么引用shell 变量

#2

帖子 枫叶饭团 » 2012-10-22 18:35

不懂,求LS解释
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: awk 怎么引用shell 变量

#3

帖子 aerofox » 2012-10-22 19:58

例子:

代码: 全选

pattern='^[0-9]+$'
awk "/$pattern/ {print}" file
头像
yanyeyy
帖子: 26
注册时间: 2008-10-06 23:04

Re: awk 怎么引用shell 变量

#4

帖子 yanyeyy » 2012-10-23 13:35

aerofox 写了:例子:

代码: 全选

pattern='^[0-9]+$'
awk "/$pattern/ {print}" file
哦,用双引号,3q~ :em11
头像
Methuselar
帖子: 122
注册时间: 2009-06-04 12:06
联系:

Re: awk 怎么引用shell 变量

#5

帖子 Methuselar » 2012-10-30 9:55

大多数时候转义双引号的代价远大于多写一个"$0 ~ pattern"

代码: 全选

print 'aaaa\nbbbb\ncccc' | awk -vPATT='aaa' '$0 ~ PATT {print}'
$和"在awk脚本里很常见,还是不贪这小方便了
Mea Culpa!
回复