Grep correct pattern with special character and variables

cat file
time="north_south_east_west_08:00" location="A" start="left" status="ok" end="north" 
time="north_south_east_west_12:00" location="C" start="right" status="ok" end="south"
time="north_south_east_west_23:00" location="G" start="left" status="ok" end="east"
time="north_south_east_west_10:40" location="K" start="top" status="ok" end="south"
time="north_south_east_west_08:50" location="P" start="top" status="x_ok" end="west"
time="north_south_east_west_09:40" location="D" start="down" status="ok" end="north"
time="north_south_east_west_11:23" location="J" start="right" status="x_ok" end="west"
time="north_south_east_west_07:45" location="E" start="left" status="ok" end="west"
time="north_south_east_west_18:10" location="E" start="down" status="ok" end="east"
time="north_south_east_west_13:55" location="O" start="left" status="x_ok" end="south"

i want the output to be printed out only coloum 1,3,5 and only either end=north/south
set var1="north"
set var2="south"

how can i integrate variable into my grep, i kind of messup with the " ' here
awk '{print $1,$3,$5}' file | grep 'end="$var1"\|end="$var2"' | less -S

expected output to be
time="north_south_east_west_08:00" start="left" end="north" 
time="north_south_east_west_12:00" start="right" end="south"
time="north_south_east_west_10:40" start="top" end="south"
time="north_south_east_west_09:40" start="down" end="north"
time="north_south_east_west_13:55" start="left" end="south"

can you provide and example of the output your after? It may be better for me to provide a solution that way?

awk '/north/||/south/ {print $1,$3,$5}' file

Please use code tags as required by forum rules!

Try

awk '/south"$|north"$/ {print $1,$3,$5}' file
time="08:00" start="left" end="north"
time="12:00" start="right" end="south"
time="10:40" start="top" end="south"
time="09:40" start="down" end="north"
time="13:55" start="left" end="south"

Including variables:

awk -v v1=$var1 -v v2=$var2 -F'"' '{if ($(NF-1)==v1||$(NF-1)==v2)  {FS=" ";print $1,$3,$5;FS="\""}}' file

Can you walk us through what awk is doing here?
Curious. :slight_smile:

updated in first thread. thanks

erm.. i miss up some string in the first post.. i have modified my file

Using variables as set in post#1:

awk -vvar1="$var1" -vvar2="$var2" '$0~(var1"|"var2)"\"$" {print $1,$3,$5}' file
time="08:00" start="left" end="north"
time="12:00" start="right" end="south"
time="10:40" start="top" end="south"
time="09:40" start="down" end="north"
time="13:55" start="left" end="south"

@briandanielz: Read it like: hand over var1 and var2 to awk; if $0 (= entire line) matches var1 OR ( | ) var2, plus double quote at the EOL ( $ ), then print the three fields.

1 Like