简体   繁体   中英

Pass variable to AWK command

I have a AWK command and i want to use it in bash script. I want to give to AWK a big text file as variable.

I have tried:

  1. cat $var | awk 'regex'
  2. echo $var | awk 'regex'
  3. awk 'regex' <<< $var

All three form does not work. When i try these command in shell awk 'regex' file it works. How can i pass a variable as Input to AWK command,i dont want to pass file to AWK . Is that possible or not.

您需要用引号引起来,以防止换行符转换为空格:

echo "$var" | awk 'regex'

Try this :

$ awk -v awkvar="$var" 'BEGIN{if (awkvar ~ "foobar"){print var}}'
foobar

您非常亲密,这也应该有效:

awk 'regex' <<< "$var"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM