0

I am facing a problem where I want awk to search for a pattern that comes from a bash variable... Now it looks like:

for i in ${CLASS_LIST[*]}; do
cat log.txt | awk -F ":" '$1 ~ /$i/ {print substr($0, index($0,$2))}'
done

But awk has no output here. If I do it like:

cat log.txt | awk -F ":" '$1 ~ /LABEL/ {print substr($0, index($0,$2))}

it works well... what is wrong in the first example?

Thanks a lot for your help in advance. regards, Joerg

thanasisp
  • 5,855
  • 3
  • 14
  • 31
  • 2
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Oct 27 '20 at 11:49
  • 1
    Take a look at awk's option `-v`. See: [Can we use shell variables in awk?](https://stackoverflow.com/q/15786777/3776858) – Cyrus Oct 27 '20 at 11:50

1 Answers1

0

Thanks for the tip, Cyrus. I've already tried the "-v" option with the same result. But I found my mistake...

when give a variable as search pattern, don't pack it in to "/"... This is working for me:

for i in ${CLASS_LIST[*]}; do
cat log.txt | awk -v var="$i" -F ":" '$1 ~ var {print substr($0, index($0,$2))}'
done

Regards, Joerg