regex - display all occurrences of match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers regex - display all occurrences of match
# 8  
Old 02-13-2009
Quote:
Originally Posted by danmauer
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)
    }
}

you don't need a 'pat' in a BEGIN block - you're going to define on a command line when you call a script - unless you want to 'hard-wire' it in a script.....
# 9  
Old 02-13-2009
Thanks.Smilie
# 10  
Old 02-16-2009
I am trying to accomplish something similar. Will this work with awk too. We do not have nawk on our server.

When I run it from $ prompt, it seems to work with the 'foo' example. But when I write it to a file and execute as

test.awk -v pat='foo[0-9][0-9]*' myfile.txt , I get the error

ksh: test.awk: not found

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

Quote:
Originally Posted by vgersh99
you don't need a 'pat' in a BEGIN block - you're going to define on a command line when you call a script - unless you want to 'hard-wire' it in a script.....
# 11  
Old 02-16-2009
make sure you have execute premissions on the file.
# 12  
Old 02-16-2009
I do.. Actually the file has 777 permissions. Is by any chance awk vs nawk making the difference ?

Quote:
Originally Posted by danmauer
make sure you have execute premissions on the file.
# 13  
Old 02-16-2009
how do you run the script?
Do you have '.' in your PATH?
Specify an absolute/relative path to the script.
# 14  
Old 02-16-2009
sorry, but i'm not sure... still learning myself.
 
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