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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk : match only the pattern string , not letters or numbers after that.
# 1  
Old 05-27-2013
awk : match only the pattern string , not letters or numbers after that.

Hi Experts,

I am finding difficulty to get exact match:

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




Code:
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:
Code:
INTERFACE_NAME[0]="lan3"


Thanks a lot,
# 2  
Old 05-27-2013
Completely hard coded, but this will work"

Code:
awk '/"lan3"/' file

# 3  
Old 05-27-2013
Code:
awk -F'=' '$2=="\"lan3\""' file

This User Gave Thanks to Yoda For This Post:
# 4  
Old 05-27-2013
Yoda, Thanks...
If I want to take lan3 in a variable, how the awk code would looks like,

Code:
L=lan3

# 5  
Old 05-27-2013
Code:
awk -F'=' -v L="lan3" '$2=="\"" L "\""' file

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

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

# 7  
Old 05-27-2013
Code:
awk -F'=' -v L="lan3" '{v=$2;gsub(/"/,X,v)}v=="lan3"' file

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. UNIX for Dummies Questions & Answers

Determine if first 2 digits of string match numbers

Trying to find out how to discover if the first 2 characters of a string are "22" Not sure how. I could use if ]; then echo "yes";fi But I think that will only grab the pattern 22 and not the first 2 digits. (5 Replies)
Discussion started by: newbie2010
5 Replies

3. Shell Programming and Scripting

Match pattern and get lines numbers in a vector

Hi everyone, I am new to shell scripting, and would appreciate your help on following problem. I need to search a file for a pattern, then get the number of each line that matches the given pattern. Then I need to search those specific line numbers from the first file in a second file and... (6 Replies)
Discussion started by: dimcick
6 Replies

4. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

5. UNIX for Dummies Questions & Answers

Selective Replacements: Using sed or awk to replace letters with numbers in a very specific way

Hello all. I am a beginner UNIX user who is using UNIX to work on a bioinformatics project for my university. I have a bit of a complicated issue in trying to use sed (or awk) to "find and replace" bases (letters) in a genetics data spreadsheet (converted to a text file, can be either... (3 Replies)
Discussion started by: Mince
3 Replies

6. UNIX for Dummies Questions & Answers

Awk pattern with letters and forward slash

Hi, I have a tab delimited file "test.txt" like this: id1 342 C/T id2 7453 T/A/-/G/C id3 531 T/C id4 756 A/T/G id5 23 A/G id6 717 T/A/C id7 718 C/T/A And so on, with the possible choices for letters being A,C,T,G. I would like to exclude from my file all the lines that do not have... (3 Replies)
Discussion started by: francy.casa
3 Replies

7. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

8. 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

9. 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

10. Shell Programming and Scripting

match numbers (awk)

i would like to enter (user input) a bunch of numbers seperated by space: 10 15 20 25 and use awk to print out any lines in a file that have matching numbers so output is: 22 44 66 55 (10) 77 (20) (numbers 10 and 20 matched for example) is this possible in awk . im using gawk for... (5 Replies)
Discussion started by: tanku
5 Replies
Login or Register to Ask a Question