Re: for awk experts only.

2008-11-30 Thread Giorgos Keramidas
On Sat, 29 Nov 2008 20:59:51 -0800, Gary Kline [EMAIL PROTECTED] wrote:
   wordnet/wn prints the string noun out whereas I'd rather it simply
   printed n.  Is there a way of making this substitution using awk?
   (I've never used awk except as a cmdline filter.)

   The following fails:

 wn foot -over |grep Overview |awk
 {if(!strcmp($3,noun))$3=n.; '{printf(%s %s\n, $4, $3);}}'

   If there are any shortcuts, please clue me in!

Don't do this with a long stream of if/else/.../else blocks.  AWK is a
pattern based rule-language.  You can apply different blocks of code to
lines that match patterns like this:

$3 ~ /adjective/ { print $1,adj. }
$3 ~ /noun/  { print $1,n. }
$3 ~ /verb/  { print $1,v. }

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: for awk experts only.

2008-11-30 Thread Gary Kline
On Sun, Nov 30, 2008 at 11:47:29AM +0200, Giorgos Keramidas wrote:
 On Sat, 29 Nov 2008 20:59:51 -0800, Gary Kline [EMAIL PROTECTED] wrote:
  wordnet/wn prints the string noun out whereas I'd rather it simply
  printed n.  Is there a way of making this substitution using awk?
  (I've never used awk except as a cmdline filter.)
 
  The following fails:
 
  wn foot -over |grep Overview |awk
  {if(!strcmp($3,noun))$3=n.; '{printf(%s %s\n, $4, $3);}}'
 
  If there are any shortcuts, please clue me in!
 
 Don't do this with a long stream of if/else/.../else blocks.  AWK is a
 pattern based rule-language.  You can apply different blocks of code to
 lines that match patterns like this:
 
 $3 ~ /adjective/ { print $1,adj. }
 $3 ~ /noun/  { print $1,n. }
 $3 ~ /verb/  { print $1,v. }

Thank you!  Would I enclose the three lines with BEGIN, and end with 
an
exit; at the end?

 

-- 
 Gary Kline  [EMAIL PROTECTED]  http://www.thought.org  Public Service Unix
http://jottings.thought.org   http://transfinite.thought.org


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: for awk experts only.

2008-11-30 Thread Giorgos Keramidas
On Sun, 30 Nov 2008 09:15:15 -0800, Gary Kline [EMAIL PROTECTED] wrote:
On Sun, Nov 30, 2008 at 11:47:29AM +0200, Giorgos Keramidas wrote:
 Don't do this with a long stream of if/else/.../else blocks.  AWK is
 a pattern based rule-language.  You can apply different blocks of
 code to lines that match patterns like this:

 $3 ~ /adjective/ { print $1,adj. }
 $3 ~ /noun/  { print $1,n. }
 $3 ~ /verb/  { print $1,v. }

 Thank you!  Would I enclose the three lines with BEGIN, and end with
 an exit; at the end?

Nope.  None of the two.

'BEGIN' has a special meaning in awk patterns.  It means run this block
of code before you start reading any input records.

If you 'exit' somewhere in a code block, then awk will terminate the
script after the first line that matches the relevant pattern.  This is
probably not quite what you want when awk filters through a long list of
words.

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


for awk experts only.

2008-11-29 Thread Gary Kline

wordnet/wn prints the string noun out whereas I'd rather it simply
printed n.  Is there a way of making this substitution using awk?
(I've never used awk except as a cmdline filter.)

The following fails:

wn foot -over |grep Overview |awk
{if(!strcmp($3,noun))$3=n.; '{printf(%s %s\n, $4, $3);}}'

If there are any shortcuts, please clue me in!


-- 
 Gary Kline  [EMAIL PROTECTED]  http://www.thought.org  Public Service Unix
http://jottings.thought.org   http://transfinite.thought.org


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: for awk experts only.

2008-11-29 Thread Polytropon
Good morning!

On Sat, 29 Nov 2008 20:59:51 -0800, Gary Kline [EMAIL PROTECTED] wrote:
   wordnet/wn prints the string noun out whereas I'd rather it simply
   printed n.  Is there a way of making this substitution using awk?
   (I've never used awk except as a cmdline filter.)
 
   The following fails:
 
 wn foot -over |grep Overview |awk
 {if(!strcmp($3,noun))$3=n.; '{printf(%s %s\n, $4, $3);}}'

Of course. You cannot have $3 as a lvalue (read: You cannot
change its value).



   If there are any shortcuts, please clue me in!

Don't make it more complicated than it is. :-)

% wn foot -over | grep Overview | awk \
'{ printf(%s %s\n, $4, gsub(noun, n., $3)); }'

And the more I think about it, the more I believe there are
much easier ways to do this. But I'm sure the magic of how it
works just opened up to you.



Sidenote: I wouldn't consider myself as an AWK expert allthough I
did abuse AWK lately to implement a stastistical evaluation program
for blood sugar data into a PDF file with diagrams a CVS file,
involving gnuplot and \LaTeX{}. =^_^=



-- 
Polytropon
From Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: for awk experts only.

2008-11-29 Thread Polytropon
Replying to my own message: I found a point for improvement.
Why use grep when awk can grep by itself?

% wn foot -over | awk '/Overview/ { printf(%s %s\n, $4, gsub(noun, n., 
$3)); }'

Ah, much better. :-)


-- 
Polytropon
From Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: for awk experts only.

2008-11-29 Thread Gary Kline
On Sun, Nov 30, 2008 at 06:17:31AM +0100, Polytropon wrote:
 Replying to my own message: I found a point for improvement.
 Why use grep when awk can grep by itself?
 
 % wn foot -over | awk '/Overview/ { printf(%s %s\n, $4, gsub(noun, n., 
 $3)); }'
 
 Ah, much better. :-)
 

Thanks for the clue[s], :)

$3 isn't only an lvalue, it's a constant.  My bad in my first try.  
What you have above prints:

foot 1  // noun
foot 0  // verb

so doesn't work entirely, but is a good start.  (BTW, man gsub turned up
nothing, so I'm assuming thhat gsub it part of awk.  And [gn]awk.)
Um, no, same with nawk, gawk, awk.

gary


 
 -- 
 Polytropon
 From Magdeburg, Germany
 Happy FreeBSD user since 4.0
 Andra moi ennepe, Mousa, ...

-- 
 Gary Kline  [EMAIL PROTECTED]  http://www.thought.org  Public Service Unix
http://jottings.thought.org   http://transfinite.thought.org


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: for awk experts only.

2008-11-29 Thread Polytropon
On Sat, 29 Nov 2008 22:52:10 -0800, Gary Kline [EMAIL PROTECTED] wrote:
   What you have above prints:
 
   foot 1  // noun
   foot 0  // verb

   so doesn't work entirely, but is a good start. 

I'm so stupid. gsub() does not return the result of the
substitution (as, for example, sprintf() would return the
string), but the success of the substitution, 1 or 0.



 (BTW, man gsub turned up
   nothing, so I'm assuming thhat gsub it part of awk.

Yes, gsub is listed in man awk because it's a function from
within awk.

I've just pkg_add'ed -r WordNet and tried:

% wn foot -over | awk '/Overview/ { printf(%s %s\n, $4, ($3 == 
noun) ? n. : ); }'
foot n.
foot

Of couse, this handles only noun. If you want to abbreviate
other kinds of words (e. g. verb - v., adverb - adv.,
adjective - adj.), it would be better to implement a short
awk script as a wrapper for the wn command. If you're only
interested in the first result mentioned, you could test NR == 1.

% wn foot -over | awk '/Overview/  (NR == 2) { printf(%s %s\n, $4, 
($3 == noun) ? n. : ); }'
foot n.



-- 
Polytropon
From Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: for awk experts only.

2008-11-29 Thread Gary Kline
On Sun, Nov 30, 2008 at 08:07:21AM +0100, Polytropon wrote:
 On Sat, 29 Nov 2008 22:52:10 -0800, Gary Kline [EMAIL PROTECTED] wrote:
  What you have above prints:
  
  foot 1  // noun
  foot 0  // verb
 
  so doesn't work entirely, but is a good start. 
 
 I'm so stupid. gsub() does not return the result of the
 substitution (as, for example, sprintf() would return the
 string), but the success of the substitution, 1 or 0.
 
 
 
  (BTW, man gsub turned up
  nothing, so I'm assuming thhat gsub it part of awk.
 
 Yes, gsub is listed in man awk because it's a function from
 within awk.
 
 I've just pkg_add'ed -r WordNet and tried:
 
   % wn foot -over | awk '/Overview/ { printf(%s %s\n, $4, ($3 == 
 noun) ? n. : ); }'
   foot n.
   foot
 
 Of couse, this handles only noun. If you want to abbreviate
 other kinds of words (e. g. verb - v., adverb - adv.,
 adjective - adj.), it would be better to implement a short
 awk script as a wrapper for the wn command. If you're only
 interested in the first result mentioned, you could test NR == 1.
 
   % wn foot -over | awk '/Overview/  (NR == 2) { printf(%s %s\n, $4, 
 ($3 == noun) ? n. : ); }'
   foot n.
 

Yeah, noun - n., verb - v., adj. - a.  adv is all 
right.
Benn awhile since I wrote an awk script... but now's the time.

thanks much,

gary


 
 
 -- 
 Polytropon
 From Magdeburg, Germany
 Happy FreeBSD user since 4.0
 Andra moi ennepe, Mousa, ...

-- 
 Gary Kline  [EMAIL PROTECTED]  http://www.thought.org  Public Service Unix
http://jottings.thought.org   http://transfinite.thought.org


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]