I'm trying to pass variable in loop with awk command to find values. I have a file:
input.txt
1234|something|ohmygod
2345|urabura|kangura
9999|1234|xxxsecrets
shell command
cat input.txt | awk -F'|' '$1 ~ /1234/'
or
awk -F'|' '$1 ~ /1234/' input.txt
get first line from file as desired. Problem occurs when I try to print this via bash. When I simply test echo like:
echo `cat input.txt | awk -F'|' '$1 ~ /1234/'`
or
echo `awk -F'|' '$1 ~ /1234/' input`
I got desired output, but unfortunately when I try to pass variable inside it
variable1="1234"
echo `awk -F'|' '$1 ~ /"$variable1"/' input`
or
variable1="1234"
echo `awk -v var="$variable1" -F'|' '$1 ~ /var/' input`
it gives one empty line. Please suggest how to pass variable inside regex awk filter.
PS It is not duplicate question to: How do I use shell variables in an awk script? due to fact that I have knowledge how to use variable in AWK as I posted up here (-v parameter) but the question is how to PASS variable in REGEX in AWK (place between two slashes - echo awk -F'|' '$1 ~ /"$variable"/' input
)