Operating System - HP-UX
1832978 Members
2976 Online
110048 Solutions
New Discussion

awk question regarding /[:space:]/ and REs

 
SOLVED
Go to solution
Graham Cameron_1
Honored Contributor

awk question regarding /[:space:]/ and REs

I am trying to use awk to (amongst other things) trap lines beginning with "--" (2 dashes). Whitespace in the form of tabs/spaces (sometimes mixed) may precede the "--".
(Yes, I am preprocessing Oracle PL/SQL code).

I am using
cat FILE |awk ' /^[:space:]*--/ {printf "%6d: %s\n", NR, $0}'
but it only detects lines beginning with "--", those with preceding whitespace are missed.
I know I could use something like
/^--/||/^ *--/||/^\t*--/
for my pattern, but this still does not pick up the arbitrary mixes of spaces and tabs so beloved of my developer community.

grep -n "^[:space:]*--" FILE does the same.

Ideas anyone ?

Graham


Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: awk question regarding /[:space:]/ and REs

Hi Graham:

Try filtering out the whitespace before, as:

cat FILE | sed 's/[ ]*//' | awk ' /--/ {printf "%6d: %s\n", NR, $0}'

Regards!

...JRF...


James R. Ferguson
Acclaimed Contributor

Re: awk question regarding /[:space:]/ and REs

Hi (again) Graham:

I should add, perhaps obviously (?) that inside the "[ ]" expression to 'sed' is a 'space' and a 'tab' character. That is, type '[', 'space', 'tab', ']'.

Regards!

...JRF...

Andreas Voss
Honored Contributor
Solution

Re: awk question regarding /[:space:]/ and REs

Hi,

to get it work only a little correction is needed, try:

cat FILE |awk ' /^[[:space:]]*--/ {printf "%6d: %s\n", NR, $0}'

(Note the [[ and ]])

Regards
Robin Wakefield
Honored Contributor

Re: awk question regarding /[:space:]/ and REs

Hi Graham,

character classes require *double* square brackets, e.g.

cat /tmp/file |awk ' /^[[:space:]]*--/ {printf "%6d: %s\n", NR, $0}'
1: -- abc
2: -- def

Rgds, Robin
Graham Cameron_1
Honored Contributor

Re: awk question regarding /[:space:]/ and REs

Andreas, Robin

Brilliant !
Thanks.
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.