Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another AWK pattern matching issue 2

Status
Not open for further replies.

Czar24

Technical User
Mar 22, 2004
16
US
I have another problem. I'm trying to extract a string to be used as part of a new file's name. For each type I know either the format of or the placement of the text.

I can't use $1 for pulling out the numerical reference since there may or may not be a space at the end of the character string I want, All I do know is that there will be a lenght to be determined numeric reference at the beginning of each line.

-SAMPLE SOURCE-

11111111111111-dont want
11111111111112-start.*101-01
11111111111113-file 1 text
11111111111114-stop
11111111111115-dont want
11111111111116-start.*101-02
11111111111117-file 2 text
11111111111118-stop
11111111111119-dont want
11111111111120-start.*101-03
11111111111121-file 3 text
11111111111122-stop
.
.
.

On lines that contain the strnig /start/ I want to grab part of the leading number as well as the series number after the "*". Ultimatly I want to use them to create files with the name: Test###-##.############## with the first few files being:
TEST101-01.11111111111112
TEST101-02.11111111111116
etc

I have been trying pattern searches such as /^[0-9]\{14\}]/ but I can't get it to work, much less assign it to a variable for future use.

Here is my code so far


nawk '
BEGIN{prefix=101}
#/start/ {datestamp=/[^[0-9]\{14\}]/}
/start/,/stop/ {file = "test" prefix "." datestamp; print >> file }
/stop/ { if (file != "") close(file) ; ++prefix}
' log1

So far my code is not generating filenames beyond "test" prefix.

Any help is appreciated. I again want to thank Aigles for his help on my other issue!



 
datestamp=substr($1, 1, index($1, "-")-1);

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
If your lines containing "start" actually end with "101-01", etc., I don't see why you need "++prefix".

[tt]
# Set field-separator to string of non-numerals.
BEGIN{ FS = "[^0-9]+" }
/-start/ { if (file) close(file)
s = substr( $0, length($0) - 5 )
file="Test" s "." $1}
/-start/,/stop$/ {print >file}
[/tt]

 
Futurelet-


I don't need ++prefix when it's all said and done. I was using that to simulate the incrimenting numbers for creating several files. I'm new to the whole awk thing, and my programming skills are rusty so I neglected to clean that up before I posted.

That code got me close enough that I got it all wrapped up. I wasn't thinking of using the substr() function, thank you for suggesting it cause it works like a champ!


Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top