For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2004 > Parsing a record









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Parsing a record
Vikas Agnihotri

2004-05-18, 7:30 am

Sounds like a trivial task for awk but I seem to have a mental block
this morning...

I have a file with full pathnames like

/full/path/name/NNN-MMM.ext

where NNN and MMM are arbitrary integers.

I want to print all the lines where MMM is greater than a specified
integer I pass in to awk (using the -v parameter)

I tried all the substr/index tricks, but there has got to be a better
regexp-based way to easily parse out the MMM part.

Help? Thanks
Ed Morton

2004-05-18, 9:30 am



Vikas Agnihotri wrote:
> Sounds like a trivial task for awk but I seem to have a mental block
> this morning...
>
> I have a file with full pathnames like
>
> /full/path/name/NNN-MMM.ext
>
> where NNN and MMM are arbitrary integers.
>
> I want to print all the lines where MMM is greater than a specified
> integer I pass in to awk (using the -v parameter)
>
> I tried all the substr/index tricks, but there has got to be a better
> regexp-based way to easily parse out the MMM part.
>
> Help? Thanks


Can you just do this:

awk -F"/-." -v mmm=5 '$(NF-1) > mmm'

Regards,

Ed.

Vikas Agnihotri

2004-05-18, 10:30 am

Ed Morton wrote:
>
> Can you just do this:
>
> awk -F"/-." -v mmm=5 '$(NF-1) > mmm'


That doesnt work. I get nothing back. $(NF-1) is the entire line
Vikas Agnihotri

2004-05-18, 11:30 am

Ed Morton wrote:

> awk -F"/-." -v mmm=5 '$(NF-1) > mmm'


Solaris's awk doesnt understand a ERE as the separator. I tried Solaris
/usr/bin/nawk and this worked. I needed to escape stuff and throw in the
| to break apart the ERE.

nawk -F "/|-|\\\." -v mmm=5 '$(NF-1) > mmm'

Thanks!
Charles Demas

2004-05-18, 2:30 pm

In article <2guj94F75h41U1@uni-berlin.de>,
Vikas Agnihotri <usenet@vikas.mailshell.com> wrote:
>Ed Morton wrote:
>
>
>Solaris's awk doesnt understand a ERE as the separator. I tried Solaris
>/usr/bin/nawk and this worked. I needed to escape stuff and throw in the
>| to break apart the ERE.
>
>nawk -F "/|-|\\\." -v mmm=5 '$(NF-1) > mmm'


try this instead:

nawk -F "[/-.]" -v mmm=5 '$(NF-1) > mmm'

IIRC, this will work too, and is easier to read/understand.

Untested.


Chuck Demas

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
Ed Morton

2004-05-18, 3:30 pm



Charles Demas wrote:

> In article <2guj94F75h41U1@uni-berlin.de>,
> Vikas Agnihotri <usenet@vikas.mailshell.com> wrote:

<snip>
>
>
> try this instead:
>
> nawk -F "[/-.]" -v mmm=5 '$(NF-1) > mmm'
>
> IIRC, this will work too, and is easier to read/understand.


It won't, but this will:

nawk -F "[-/.]" -v mmm=5 '$(NF-1) > mmm'

The slash is "special" when it appears first.

Regards,

Ed.

> Untested.
>
>
> Chuck Demas
>


Dave Thompson

2004-05-26, 10:31 pm

On Tue, 18 May 2004 13:16:53 -0500, Ed Morton <morton@lsupcaemnt.com>
wrote:
<snip>
>
> It won't, but this will:
>
> nawk -F "[-/.]" -v mmm=5 '$(NF-1) > mmm'
>
> The slash is "special" when it appears first.
>

Not the slash. Hyphen is special in a charclass when it is embedded --
neither first (except optionally uparrow) or last. [/.-] also works.


- David.Thompson1 at worldnet.att.net
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com