how can awk match multi pattern in a string


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users how can awk match multi pattern in a string
# 1  
Old 03-13-2008
Question 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:
[string1]bin[string2]rundb[string3]
the string1, 2 & 3 may contain word, number, blank or "/". The "bin" should be ahead "rundb"

My question is how i can pick up this string by matching "bin" and "rundb" in one regular expression....

Sounds it like a normal question for awk, but i could not find it from the google. Please kindly provide your answer or suggestion here. Thanks in adv.

Last edited by sleepy_11; 03-14-2008 at 12:23 AM..
# 2  
Old 03-14-2008
Code:
awk '$0 ~ /bin.*rundb/'

# 3  
Old 03-14-2008
Thanks, it works perfectly. and easier than the way i tried.....
# 4  
Old 03-14-2008
another issue comes out here. As the patter keeps change, I want to put it into a variable and reference the variable in a regular expression. Like below:
key="bin.*rundb"
if (fieldname ~ /key/) {
printf("%s\n", fieldname);
}

I tried "'$key'", '"$key" and $key in the regular expression, but all won't work. Do you know how can i accomplish it?
# 5  
Old 03-14-2008
Try this:

Code:
awk -v k=$key '$0 ~ k'

Regards
# 6  
Old 03-17-2008
Got some unexpected answer below:
$ nawk -v k="ora_qmn0_sid" '$0 ~ k' sample.txt
28321 65548 1 59 0 569M 533M sleep 0:00 0 0.00% 0.00% 0.00% 0.00% usr mc_off ora_qmn0_sid
$ awk -v k="ora_qmn0_sid" '$0 ~ k' sample.txt
awk: syntax error near line 1
awk: bailing out near line 1
$ awk -v k="ora_qmn0_sid" '$0 ~/"'$k'"/' sample.txt
awk: syntax error near line 1
awk: bailing out near line 1

Seems that the "-v" option does not work on solaris 9&10. I also try above "awk -v" command on a linux box, and it works fine......a little bit weird to me.

but another issue i met is that '$0 ~ k' works but $0 ~/"'$k'"/ not
{
fieldname=""
key="ora_.*_sid"

# if (fieldname ~ /"'$k'"/) {
if (fieldname ~ key) {
printf("%s\n", fieldname);
}
}
The commented line won't work as the one below it. Does anyone know the reason?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE="" DHCP_ENABLE=0 INTERFACE_NAME="lan3:1"... (6 Replies)
Discussion started by: rveri
6 Replies

2. Shell Programming and Scripting

Pattern Match & Extract from a string

Hi, I have long string in 2nd field, as shown below: REF1 | CLESCLJSCSHSCSMSCSNSCSRSCUDSCUFSCU7SCV1SCWPSCXGPDBACAPA0DHDPDMESED6 REF2 | SBR4PCBFPCDRSCSCG3SCHEBSCKNSCKPSCLLSCMCZXTNPCVFPCV6P4KL0DMDSDSASEWG I have a group of fixed patterns which can occur in these long strings & only... (11 Replies)
Discussion started by: karumudi7
11 Replies

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

4. Shell Programming and Scripting

pattern match in a string

Hello, Please see below line code: #!/bin/ksh set -x /usr/bin/cat /home/temp |while read line do if ] then echo "matched" else echo "nope" fi done content of filr temp is as below (4 Replies)
Discussion started by: skhichi
4 Replies

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

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

Match elements in an AWK multi-dimensional array

Hello, I have two files in the following format; file1: A B C D E F G H I J K L file2: 1 2 3 4 5 6 7 8 9 10 11 12 I have read them both in to multi-dimensional arrays. I need a file that has column 2 of the first file printed out for each column 3 of the second file ie... ... (3 Replies)
Discussion started by: cold_Que
3 Replies

8. Shell Programming and Scripting

Date Pattern Match (replacement string)

Hello, i am splitting files and sometimes the string of the pattern doesnt exist in the input file it starts for example with 00:00:01. So the output is completely desorganized, is there any way of putting a replacement string in the pattern so it will grab all the times from 00:**:** to first... (0 Replies)
Discussion started by: x-plicit78
0 Replies

9. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

Match pattern and replace with string

hi guys, insert into /*<new>*/abc_db.tbl_name this is should be replaced to insert into /*<new>*/${new}.tbl_name it should use '.' as delimiter and replace is there any way to do it using sed (6 Replies)
Discussion started by: sol_nov
6 Replies
Login or Register to Ask a Question