awk - Print where value is in quotes

Hi All,

I have input data like follows:

"1234"|"ABC"
"1234"|"CBA"
"1222"|"ZZZ"

I am trying to awk print all records where Col1 = "1234" .

Below is the code I have so far:

Var1=1
Var2=1234

awk -F "|" "$ ${Var1} == "\"${Var2}\"" { print; }' inputfile

However when the AWK command is running, it is not enforcing that I want to find data which = "1234" , it is just looking for data which = 1234 (i.e. without the double quotes).

Note -- The above command works fine from command line, however when running from a script, the "\ is being removed, therefore not enforcing that I want to find data with double quotes around.

Can anyone assist? :slight_smile:

Your quoting is wrong in several places. Try

awk -F "|" "\$${Var1} == \"\\\"${Var2}\\\"\"" file
"1234"|"ABC"
"1234"|"CBA"

which is not the recommended way to pass variables...

Note that the recommended way to pass variables would be:

Var1=1
Var2=1234

awk -F "|" -v field="$Var1" -v value="$Var2" '$field == "\"" value "\""' inputfile
1 Like