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

Interesting 'awk' problem

Status
Not open for further replies.

Gafling

Programmer
Nov 7, 2000
44
0
0
US
I think I need a real 'regular expression' guru here ...

using this data ( = escape char):
(15Y(s1p12v0s0b0T*757540097970*(10U&l6D(s0p11h0s0b4099T

I apply the following 'awk' rule:
/\033.*\*/ { sub(/\033.*\*/,"") }

In my experience, this should locate a line with the target data and then
delete the data beginning with the left-most escape character up thru the
FIRST '*'. The target lines are properly located but when the 'sub'
function is run, the data is deleted thru the second '*'. I am sure the
second (escaped) '*' in the replacement function is getting the regular
expression handler confused but I cannot seem to find out how to correct
the substitution so it can work properly.

I would appreciate anyone's insight ...
 
Hello Gafling,

the problem you are facing here is due to the fact that
regular expresions are "greedy" (if they can "eat" more
will do), i.e. the matched string for a pattern will be the
longest possible.

In this case, there is an alternative rule that will do the
work you need. Try with this:

/\033.*\*/ {sub(/[^*]*\*/,""}

the substitution will look for a string of any characters
except * followed by an *, and deletes it.

If you deal regularly with problems like this, take a look
on Perl. It has very strong support for regular expressions.

regards.

Jose Manuel Blanco
BrainBench MVP for Unix admin.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top