![]() |
Pattern matching in a text file - use of AWK??
I need to do some scripting to read through a text file, and find the last occurrence of a word in the file that corresponds to a look up list. When that line containing that word has been found, I need to extract out the last numerical character from that line, and substitute it for another character in another text file which will then be appended to the first.
e.g. the original file will contain something like ARCHIVE 1 store begin ********************************* * Retrieve interface by default into INTERFACE001 ARCHIVE 1 retrieve begin ********************************* CRITIC 1 2 what I want to do is find CRITIC, since thats the last occurrence of one of the words in my lookup list. I need to then extract out the number 2, and substitute that for something like x in another text file. Guess I can do the last part using sed. But should I use AWK or GREP for the first bit. W |
Quote:
|
. . . and to get the last occurrence, of your grep output, pipe it to tail
like this: grep CRITIC filename | tail -n 1 then pipe all that to sed/awk |
Or in awk
Code:
awk '/CRITIC/{line=$0} END{$0=line; print $NF}' file |
Moin,
Quote:
Code:
tac file | awk '/CRITIC/{print $NF; exit;}' |
Good idea - would be worthwhile to time executions.
|
You're all ignoring the "list of words in another file" part of the OP's problem.
Consider this possibility: Code:
$ cat fields:eek: Sorry. There's an error in this code. See my post below for commented corrected code. </edit> Using this code: PHP Code:
|
Quote:
Quote:
the criteria for that replacement might be, either. Cheers, Tink |
Um, Tink, look in the last quote you posted: "... since that's one of the words in my lookup list." I think that's a fairly clear indication that the OP had a "list" of words, not just a single word, in mind. The "CRITIC" part was just an example of a match from the list. (That's why I used a two-word list in my example code.)
<edit> And so I looked at my code and realized I was reporting $ NF, which is the last field in the last line of the file, not the matching line. :o Here's a corrected version of the the code with some added comments: PHP Code:
|
Ok, thanks, thats works a treat! I did mean a word from a lookup list...........sorry if it was a bit vague to earlier posters
Can I bed this within another parent script, and if so what would the syntax be? The parent script cd's to a specific directory(supplied at the commandline), and spools through all files, performing various actions. This above is the first action, and the output from that will be used to substitute for characters in another block of text, which will ultimately be appended to the orig file. Hope thats clear! |
As to the embedding, if you're using a bash shell, you are, in effect, already embedded. . . :D
Anyhow, the syntax is the same as it would be on a command line. Something like this: Code:
#/bin/bash |
Ok, that works. Ta v much
|
I need to ensure this awk script just carries out the matching process with the first word on the line. Currently it looks for the last occurrence of a word anywhere on the line, which is messing up my results
The current syntax is Code:
if (match($0, words, val)) { # Use the "match" function to extract the matched stringAny help gratefully received |
Yes, substituting $1 for $0 in the call to the match function will match the regular expression in words to the first input field rather than the whole input line.
|
I now note that the script will only pick up matches in the same case. If I wanted to look for matches in either upper or lower case, and the list to lookup against is in uppercase, do I have to add words in lowercase?
W |
| All times are GMT -5. The time now is 09:19 AM. |