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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ignoring part of text when matching

Status
Not open for further replies.

AA2K5

Programmer
Apr 24, 2002
31
GB
Hi,

I have a problem when doing some pattern matching.

Basically I'm trying to insert a new line whenever I come across a certain word i.e. WORD. But if the text is enclosed in some brackets for example, then I want to leave it as it is.

At the moment I am applying the pattern to WORD when it is found but I am ignoring anything in brackets by skipping to the next line. This approach doesn't work if I have the following text - "(Some text with WORD) and WORD". Because the line is skipped as it has brackets.

I'm sure theres a simple way of doing this but I'm new to awk so I'm struggling. By the way I am using gawk on windows. Any help would be appreciated.


 
A starting point:
x=$0;gsub(/\([^)]*\)/,"",x)
if(x~/WORD/) ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Is the code above correct or did you mean: -

x=$0;gsub(/\([^[red]\[/red])]*\)/,"",x)
if(x~/WORD/) ...

Also if I understand the above correctly you are stripping out anything containing brackets and storing it into x. So then any further processing is done on x not on $0.

But if I still want to output anything that was in brackets this is in x and not in $0. How can I output the stuff in brackets and still force a newline after WORD? For example this : -

(Some text with WORD) and WORD and some more text

becomes this: -

(Some text with WORD) and WORD
and some more text


Any help?
 
And this ?
{ x=$0;gsub(/WORD/,"&\n",x)
while(match(x,/\([^)]*\n/))
x=substr(x,1,RLENGTH-1)""substr(x,RLENGTH+1)
print x
}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for that.

It looks good. I'll give it a try when I get a chance.

Thanks a lot for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top