awk : match the string and string with the quotes :


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk : match the string and string with the quotes :
# 1  
Old 05-27-2013
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 :
Code:
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:

Code:
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:

Code:
# 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
Code:
grep lan3 file

, and was not getting the result.

Thanks all..
# 2  
Old 05-27-2013
You can also do this with grep:
Code:
grep -E '("lan3")|(=lan3$)' file

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-27-2013
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 :

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

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


Using awk:
Code:
# 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk, if line after string does not match insert

I have a large file with interface records. I need to check every record that has the string "encapsulation bridge1483" and if the next line after this does not have "ip description" then I need to insert a line to add "ip description blah_blah_blah. Sample file: interface atm 1/0.190158... (3 Replies)
Discussion started by: numele
3 Replies

2. Shell Programming and Scripting

Unable to match string within awk printf

Hi All I am working to process txt file into csv commo separated. Input.txt 1,2,asdf,34sdsd,120,haahha2 2,2,wewedf,45sdsd,130,haahha ..... .... Errorcode.txt 120 130 140 myawk.awk code: { BEGIN{ HEADER="f1,f2,f3,f4,f5,f6" (4 Replies)
Discussion started by: krsnadasa
4 Replies

3. UNIX for Dummies Questions & Answers

awk for trimming a string up to the first, then second, then third... match

Hi ! With awk, I would need to trim a string from the beginning up to the first occurrence of "1", then from the beginning up to the second occurrence of "1", then from the beginning up to the third, then the fourth...., then the last occurrence of "1". input: 1aaa1bb1ccccccc dd1e1ffff... (7 Replies)
Discussion started by: beca123456
7 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

match string exactly with awk/sed

Hi all, I have a list that I would like to parse with awk/sed. The list is contains entries such as: JournalTitle: Biochemistry JournalTitle: Biochemistry and cell biology = Biochimie et biologie cellulaire JournalTitle: Biochemistry and experimental biology JournalTitle: Biochemistry and... (6 Replies)
Discussion started by: euval
6 Replies

6. Shell Programming and Scripting

AWK break string into fields + pattern match

I am trying to break a string into separate fields and print the field that matches a pattern. I am using awk at the moment and have gotten this far: awk '{for(i=1;i<=NF;++i)print "\t" $i}' longstring This breaks the string into fields and prints each field on a separate line. I want to add... (2 Replies)
Discussion started by: Moxy
2 Replies

7. Shell Programming and Scripting

How to get part of string in awk from match

Hi, Im an awk noob and I am having trouble trying to get matches. Here is my script: #!/bin/gawk -f BEGIN {} $0 ~ /<a href=".*">.*<\/a>/{print} Ideally I want to be able to get the actual link and print it. In PHP you can do preg_replace and get the match you want by using \\1 where 1... (2 Replies)
Discussion started by: adsyuk
2 Replies

8. Shell Programming and Scripting

How can I match lines with just one occurance of a string in awk?

Hi, I'm trying to match records using awk which contain only one occurance of my string, I know how to match one or more (+) but matching only one is eluding me without developing some convoluted bit of code. I was hoping there would be some simple pattern matching thing similar to '+' but... (9 Replies)
Discussion started by: jonathanm
9 Replies

9. Shell Programming and Scripting

Awk:Find length of string omitting quotes

Hi , I have a file named "sample" having the data as follows. "663005487","USD",0,1,"NR" If i give like a=`awk -F ',' '{printf length($2)}' sample` (Trying to find length of second field)I should get the output for the above as 3 (Omitting double quotes) not 5. How to do this..... (2 Replies)
Discussion started by: jayakumarrt
2 Replies

10. UNIX for Advanced & Expert Users

how can awk match multi pattern in a string

Hi all, I need to category the processes in my system with awk. And for now, there are several command with similar name, so i have to match more than one pattern to pick it out. for instance: binrundb the string1, 2 & 3 may contain word, number, blank or "/". The "bin" should be ahead "rundb"... (5 Replies)
Discussion started by: sleepy_11
5 Replies
Login or Register to Ask a Question