简体   繁体   中英

How do I use awk to match multiple variable patterns?

I want to use awk to match multiple variable patterns. Here is what I have so far:

match=`awk -v "$var1\|$var2\|$var3" 'BEGIN{FS=":"; OFS="-"}
$2 ~ {print}' $file`

Any help is appreciated.

You need to pass 3 variable separately using awk -v var1=val1 syntax and then use alternation inside the awk regex as this one:

match=$(awk -v v1="$var1" -v v2="$var2" -v v3="$var3" 'BEGIN{FS=":"; OFS="-"}
$2 ~ v1 "|" v2 "|" v3' "$file")

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