regex - display all occurrences of match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers regex - display all occurrences of match
# 1  
Old 02-13-2009
regex - display all occurrences of match

i don't want to display the whole line but i want to display all the string(s) that match the Regex, even if their are more then one match per line in my file.

data:

mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/wkly_inqry.sql $AI_SQL/wkly_inqry_trtry.sql $AI_SQL/wkly_nb_trtry.sql \
mds_ar/bin/uedw92wp.ksh:$AI_SQL/wkly_new_bsns.sql $AI_SQL/wkly_runstats.sql $AI_SQL/wkly_mqt.sql \
mds_ar/bin/uedw92wp.ksh:$AI_SQL/wkly_prtl_bnd.sql $AI_SQL/wkly_prtl_bnd_st.sql $AI_SQL/wkly_prtl_inqry.sql \
mds_ar/bin/uedw92wp.ksh:$AI_SQL/wkly_prtl_inqry_st.sql $AI_SQL/wkly_prtl_nb.sql $AI_SQL/wkly_prtl_nb_st.sql > $EDW_TMP/wkly.sql
mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/mnthly_mqt.sql $AI_SQL/actl_pln.sql > $EDW_TMP/mthly.sql

the following code: only gives me the first match per line... i want to see each match from the same line too.

Code:
#!/usr/bin/nawk -f
# demonstrate the match function

BEGIN {
     regex="AI_SQL\/[a-zA-Z_]+\.sql"
}
{
    if (match($0,regex)) {
#           RSTART is where the pattern starts
#           RLENGTH is the length of the pattern
            before = substr($0,1,RSTART-1);
            pattern = substr($0,RSTART,RLENGTH);
            after = substr($0,RSTART+RLENGTH);
#            printf("%s\n<%s>\n%s\n", before, pattern, after);
           printf("<%s>\n", pattern);
    }
}


Last edited by danmauer; 02-13-2009 at 07:38 PM.. Reason: add more details
# 2  
Old 02-13-2009
myFile being:
Code:
asdfkj foo2 bar foo1
foo100 fo bee dah foo5

Trying to get all 'foo<numbers>' patterns
Code:
nawk -v pat='foo[0-9][0-9]*' '
{
    while (match($0, pat)) {
       printf substr($0, RSTART, RLENGTH) OFS
       $0=substr($0, RSTART+RLENGTH)
    }
    print ""
}' myFile


Last edited by vgersh99; 02-13-2009 at 07:42 PM.. Reason: indents
# 3  
Old 02-13-2009
yeah, you're pretty close - the format you want:
Code:
nawk -v pat='foo[0-9][0-9]*' '
{
    while (match($0, pat)) {
       printf("<%s>\n", substr($0, RSTART, RLENGTH))
       $0=substr($0, RSTART+RLENGTH)
    }
}' myFile


Last edited by vgersh99; 02-13-2009 at 07:51 PM.. Reason: ooops - missing paren
# 4  
Old 02-13-2009
Thanks, i got it to work in line.... but i'm having trouble with the syntax to put it in a script...

Code:
 
#!/usr/bin/nawk -f
 
 
#BEGIN {
#     pat="AI_SQL\/[a-zA-Z_]+\.sql"
#}
 
nawk -v pat='AI_SQL\/[a-zA-Z_]+\.sql' '
{
    while (match($0, pat)) {
       printf("%s\n", substr($0, RSTART, RLENGTH) OFS);
       $0=substr($0, RSTART+RLENGTH)
    }
}' ?????

# 5  
Old 02-13-2009
chmod +x dan.awk

dan.awk -v pat='AI_SQL\/[a-zA-Z_]+\.sql' myFile2parse

dan.awk:
Code:
#!/usr/bin/nawk -f
{
    while (match($0, pat)) {
       printf("%s\n", substr($0, RSTART, RLENGTH) OFS);
       $0=substr($0, RSTART+RLENGTH)
    }
}

# 6  
Old 02-13-2009
GOT it.....

Code:
 
#!/usr/bin/nawk -f
 
BEGIN {
pat="AI_SQL\/[a-zA-Z_]+\.sql"
}
 
{
    while (match($0, pat)) {
       printf substr($0, RSTART, RLENGTH) OFS;
       $0=substr($0, RSTART+RLENGTH)
    }
}

# 7  
Old 02-13-2009
Awsome ! Thanks again.

Awsome ! Thanks again.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the number of string occurrences to display 0 entries in output

Hello Friends, Can somebody assist an issue I am having? I have a separate file with a list of account ids XXX200B02Y01 XXX200B03Y01 XXX200B05Y01 XXX200B07Y01 XXX200B08Y01 I call the file, and run an egrep against a directory and logfiles AccountID=$(cat... (2 Replies)
Discussion started by: liketheshell
2 Replies

2. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

3. Shell Programming and Scripting

Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget)... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. UNIX for Advanced & Expert Users

sed REGEX to print multiple occurrences of a pattern from a line

I have a line that I need to parse through and extract a pattern that occurs multiple times in it. Example line: getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed,... (4 Replies)
Discussion started by: Vidhyaprakash
4 Replies

5. Shell Programming and Scripting

Print occurrences for pattern match

Hi All, I want to print all the occurrences for a particular pattern from a file. The catch is that the pattern search is partial and if any word in the file contains the pattern, that complete word has to be printed. If there are multiple words matching the pattern on a specific line, then all... (2 Replies)
Discussion started by: decci_7
2 Replies

6. Shell Programming and Scripting

Only Regex pattern match help

Hi We have a tool to monitor logs in our environment. The tool accepts log pattern match only using regex and I accept I am a n00b in that:confused:. I had been banging my head to make it work without much success and at last had to turn on to my last option to post it here. I had got great... (2 Replies)
Discussion started by: radioactive9
2 Replies

7. Shell Programming and Scripting

Regex find first 5-7 occurrences of a set of digits within a string

Using these strings as an example: <a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=145148&amp;playTogether=True',960,540,943437);return false;" title=""> <a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=1451486&amp;playTogether=True',960,540,94343);return false;" title=""> <a... (12 Replies)
Discussion started by: metallica1973
12 Replies

8. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

9. UNIX for Advanced & Expert Users

Regex to match IP address

What do you think of this regex to match IP address? I have been reading up on regex and have seen some really long ones for IP. Would this fail in any scenarios? (+\.){3}* (5 Replies)
Discussion started by: glev2005
5 Replies

10. Shell Programming and Scripting

regex to match basename

Hi Can somebody please help me know how do i match the basename using a regular expression using posix standard in shell script suppose i want to match /u01/Sybase/data/master.dbf the result should be master.dbf as i want to match everything after the last / regards (8 Replies)
Discussion started by: xiamin
8 Replies
Login or Register to Ask a Question