awk : match only the pattern string , not letters or numbers after that.

Hi Experts,

I am finding difficulty to get exact match:

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
ubuntu: awk '/lan3/' file 
INTERFACE_NAME[0]="lan3"
INTERFACE_NAME[1]="lan3:1"

ubuntu:grep -w "lan3"  file
INTERFACE_NAME[0]="lan3"
INTERFACE_NAME[1]="lan3:1"

How to get rid of the lan3:1 in the output, as I dont want that .

The output should be:

INTERFACE_NAME[0]="lan3"

Thanks a lot,

Completely hard coded, but this will work"

awk '/"lan3"/' file
awk -F'=' '$2=="\"lan3\""' file
1 Like

Yoda, Thanks...
If I want to take lan3 in a variable, how the awk code would looks like,

L=lan3
awk -F'=' -v L="lan3" '$2=="\"" L "\""' file
1 Like

Yoda, Thanks worked perfectly , for me it was messed up with space & ".
Now it is working. Thanks a lot.

---------- Post updated at 03:21 PM ---------- Previous update was at 03:02 PM ----------

Someof the file the quote inside the lan is not there:

The data is like this:

  • Can there be a common syntax to find the only lan3 entry , in the output
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[3]=lan2
IP_ADDRESS[3]=10.53.50.58
SUBNET_MASK[3]=255.255.254.0
BROADCAST_ADDRESS[3]=
INTERFACE_STATE[3]=
DHCP_ENABLE[3]=0
awk -F'=' -v L="lan3" '{v=$2;gsub(/"/,X,v)}v=="lan3"' file
1 Like