awk : match the string and string with the quotes :

Hi all,

Here is the data file:

  • want to match only lan3 in the output .
  • not lan3:1

file :

OPERATING_SYSTEM=HP-UX
LOOPBACK_ADDRESS=127.0.0.1

INTERFACE_NAME[0]="lan3"
IP_ADDRESS[0]="10.53.52.241"
SUBNET_MASK[0]="255.255.255.192"
BROADCAST_ADDRESS[0]=""
INTERFACE_STATE[0]=""
DHCP_ENABLE[0]=0

INTERFACE_NAME[1]="lan3:1"
IP_ADDRESS[1]="10.53.52.240"
SUBNET_MASK[1]="255.255.255.192"
BROADCAST_ADDRESS[1]=""
INTERFACE_STATE[1]=""
DHCP_ENABLE[1]=0

INTERFACE_NAME[10]=lan3
IP_ADDRESS[10]=10.53.52.241
SUBNET_MASK[10]=255.255.255.192
BROADCAST_ADDRESS[10]=
INTERFACE_STATE[10]=
DHCP_ENABLE[10]=0

INTERFACE_NAME[11]=lan3:1
IP_ADDRESS[11]=10.53.52.240
SUBNET_MASK[11]=255.255.255.192
BROADCAST_ADDRESS[11]=
INTERFACE_STATE[11]=
DHCP_ENABLE[11]=0

INTERFACE_NAME[13]=lan2
IP_ADDRESS[13]=10.53.50.58
SUBNET_MASK[13]=255.255.254.0
BROADCAST_ADDRESS[13]=
INTERFACE_STATE[13]=
DHCP_ENABLE[13]=0

The output should be:

INTERFACE_NAME[0]="lan3"
INTERFACE_NAME[10]=lan3

Thanks.

---------- Post updated at 03:41 PM ---------- Previous update was at 03:32 PM ----------

I got it correct this time from the previous helps:

# awk -F'=' '{for(i=1;i<=NF;i++) if(($i~"lan3$")||($i~"lan3\"$")) print $0}' file
INTERFACE_NAME[0]="lan3"
INTERFACE_NAME[10]=lan3

I never thought this will be so tricky and need to use awk , I waj just using

grep lan3 file

, and was not getting the result.

Thanks all..

You can also do this with grep:

grep -E '("lan3")|(=lan3$)' file
1 Like

Don, Thanks it worked with grep as you have mentioned:

---------- Post updated at 04:21 PM ---------- Previous update was at 04:19 PM ----------

Also figured out it using variable like : L=lan3 :

grep -E '('$L'$)|('$L'"$)' file1

INTERFACE_NAME[0]="lan3"
INTERFACE_NAME[10]=lan3

Using awk:

# L=lan3 ; awk -F'=' -v l1=$L '{for(i=1;i<=NF;i++) if(($i~/'$L'$/)||($i~/'$L'"$/)) print $0}' file1
INTERFACE_NAME[0]="lan3"
INTERFACE_NAME[10]=lan3

Thanks for the help.