NAWK - looping with match() function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting NAWK - looping with match() function
# 1  
Old 02-16-2009
NAWK - looping with match() function

i'm trying to use the "before" output from the match() function as part of the results of each Regex match... but...

My input data: (from an input file)

i only show the first record in my file.. all other records are similar.

mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/wkly_inqry.sql $AI_SQL/wkly_inqry_trtry.sql $AI_SQL/wkly_nb_trtry.sql \


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

I was expecting to see the "before" in my printf() literaly before "pattern" each time i cycle through the while loop, but i'm only getting it the first time it loops through.. Smilie

Results i'm getting for above code:

mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/wkly_inqry.sql
$AI_SQL/wkly_inqry_trtry.sql
$AI_SQL/wkly_nb_trtry.sql


Results i'm ultimately trying to generate:

mds_ar/bin/uedw92wp.ksh:$AI_SQL/wkly_inqry.sql
mds_ar/bin/uedw92wp.ksh:$Ai_SQL/wkly_inqry_trtry.sql
mds_ar/bin/uedw92wp.ksh:$AI_SQL/wkly_nb_trtry.sql
# 2  
Old 02-16-2009
no, because you're overwriting the the whole record/line on every iteration through the 'while' loop - chopping it up to 'nothing' with 'match' and the '$0=...' assignment.

An alternative - holding on to the FIRST 'before' pattern and printing it out for every 'match':
Code:
#!/usr/bin/nawk -f

BEGIN {
pat="AI_SQL\/[a-zA-Z_]+\.sql"
}

{
    if (match($0, pat)) {
       before = substr($0,1,RSTART-1);

       do {
          pattern = substr($0,RSTART,RLENGTH);
          printf("%s%s\n", before, pattern);
          $0=substr($0, RSTART+RLENGTH)
       } while (match($0, pat))
    }
}

# 3  
Old 02-16-2009
Thank you very much.... my understand is increasing... thanks for the help...Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk pattern match by looping through search patterns

Hi I am using Solaris 5.10 & ksh Wanted to loop through a pattern file by reading it and passing it to the awk to match that value present in column 1 of rawdata.txt , if so print column 1 & 2 in to Avlblpatterns.txt. Using the following code but it seems some mistakes and it is running for... (2 Replies)
Discussion started by: ananan
2 Replies

2. Shell Programming and Scripting

Nawk match regex of bash variable

Using a bash for loop to pass variables into a nawk loop to capture a string in an sftp log. Tried several different syntax methods to have the variable treated as a regex so the loop will capture the string. for i in `cat /tmp/dar3.out.2` do nawk -vst=$i '$5 ~ /$st/ && /closed/ && /user/... (3 Replies)
Discussion started by: smenago
3 Replies

3. Shell Programming and Scripting

Help with nawk (Exact Match)

I have a file with the contents below "lets say the name of the file is abcxyz" shown at the end of this. I am using nawk to find the exact ip address and the 6 lines after the match is found using the following nawk statement /usr/bin/nawk "/111.46.14.107/,printed==6 { ++printed; print; }"... (7 Replies)
Discussion started by: knijjar
7 Replies

4. Shell Programming and Scripting

Use match() in nawk to find digits in number

Hi, I just need to check whether number of digits in a phone number is 10 or not. If I am not wrong regex will be: {9} I have to use this inside nawk as this is a small portion of a big program. nawk ' BEGIN { RS="";FS=";"; regex="{9}"; } { for (i=1;i<=NF;i++) { if... (6 Replies)
Discussion started by: shekhar2010us
6 Replies

5. Shell Programming and Scripting

C Shell Script: While function not fully looping

I am new to scripting and this is probably the 4th or 5th simple script I have written. I am working with a HUGE number of data that need to be organized into folders and named a certain way. I wrote the naming script using a while function to go through the 1000-some folders and rename the files... (0 Replies)
Discussion started by: notluckyhannah
0 Replies

6. Shell Programming and Scripting

Help using SYSTEM function in NAWK

I am scanning a file (line by line) for format errors. A line could have multiple errors. Each field in the line is evaluated for errors and sent, along w/ any error messages, to a temporary file. Finally, if any errors were detected, this temporary file is then appended to the errorFile. The... (4 Replies)
Discussion started by: aschera
4 Replies

7. Shell Programming and Scripting

match 2 files using nawk command

i am matching two files. the files are in the format file_1.txt 1|_|X|_| 2|_|W|_| 3|_|Y|_| 4|_|Z|_| 5|_|U|_| file_2.txt W|_|A|_| Z|_|C|_| V|_|B|_| X|_|D|_|sdff|_| Y|_| file_3.txt should be in the format (4 Replies)
Discussion started by: centurion_13
4 Replies

8. Shell Programming and Scripting

NAWK getline function

Hi, I am using a Solaris OS here. My intention is to print the 2nd field if the first field matches "APPLE=". I am using the "getline" function here (shown below), but it doesn;t work. Can any experts give me some advice? Input: ORANGE= 10 APPLE= 20 MANGO= 30 GRAPES= 40 Output: 20 ... (5 Replies)
Discussion started by: Raynon
5 Replies

9. Shell Programming and Scripting

Nawk Time Function

Hi All, I am using solaris and nawk. Is there any time function in nawk which is simliar to the shell `date` function ? Can any experts show any examples? (4 Replies)
Discussion started by: Raynon
4 Replies

10. Shell Programming and Scripting

Nawk user-defined function

HELP!!!! I am in an on-line shell programming class and have a question. Here is the data: Mike Harrington:(510) 548-1278:250:100:175 Christian Dobbins:(408) 538-2358:155:90:201 Susan Dalsass:(206) 654-6279:250:60:50 (There are 12 contribuors total) This database contains names, phone... (1 Reply)
Discussion started by: NewbieGirl
1 Replies
Login or Register to Ask a Question