Unix/Linux Forum: How to nest an awk into another awk? - Unix/Linux Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to nest an awk into another awk? Rate Topic: -----

#1 User is offline   UnIxBug 

  • Newbie (User Level: 0/10)
  • Group: Members
  • Posts: 2
  • Joined: 24-February 09

Posted 24 February 2009 - 04:58 PM

Hi all,

waht I am trying to do is to invoke an awk inside another, using the system() call from awk (along with other commands) but it will return only errors like "^ unterminated string" , "^ unexpected newline", or syntaxes errors.

The small script I am trying to do would look like:

---------------

#!/bin/bash
#!/bin/awk -f


ls -lrt /var/log * >& 1 | egrep "log" | sort -m | awk '{print strftime("%y %m %d")"\t", "/var/log/"$9"\t",
substr($8,3,4)"\t", system("cal -y | awk 'NF==3 {printf "%s\n%s\n%s\n",$1,$2,$3}' | cat -n | grep $6 | cut 1")"\t", $7}'

----------------


But if executed returns the following error:

awk: cmd. line:2: substr($8,3,4)"\t", system("cal -y | awk NF==3
awk: cmd. line:2: ^ unterminated string



Both sentences, if executed apart, work:

$ls -lrt /var/log * >& 1 | egrep "log" | sort -m | gawk '{print strftime("%y %m %d")"\t", "/var/log/"$9"\t", substr($8,3,4)"\t", $6"\t", $7}'

would give as output:


...
09 02 24 /var/log/[logfilename] 06 Mar 25
...


and

$cal -y | awk 'NF==3 {printf "%s\n%s\n%s\n",$1,$2,$3}' | cat -n | grep Feb | cut -f1

transforms the abbreviated month into his numeric

$2

What I am intending to do is that for each output of the first awk, the abbreviated month that the "ls -lrt" returns is transformed into its numerical by the second command sentence:


09 02 24 /var/log/[logfilename] 06 Mar 25 -----------> 09 02 24 /var/log/[logfilename] 06 3 25


Any idea why the second awk does not work inside trhe system() call or anyone has a suggestion on achieving the conversion of the month from abbreviated to numeric?

Thanks in advance for any help!
0

#2 User is offline   eggi 

  • User Level: 6/10
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 311
  • Joined: 25-November 07
  • Location:Grayslake, IL USA

Posted 25 February 2009 - 03:23 AM

Hey there,

Actually, if i'm not mistaken, even if you get that to work, awk's system function only sends back the return code (did it work or not), won't help you.

One thing you could do is to assign the output of your nested awk command to a shell variable and then use awk's -v flag to pass that variable to your outer awk script - something like this (can't test it, but it should give you the idea :)

variable=`awk $cal -y | awk 'NF==3 {printf "%s\n%s\n%s\n",$1,$2,$3}' | cat -n | grep Feb | cut -f1 SOME_FILE_NAME`


awk -v var=variable '{print strftime("%y %m %d")"\t", "/var/log/"$9"\t", var, substr($8,3,4)"\t", $6"\t", $7}'


It's not even close to working the way you want, just an example of how can insert awk into awk :)

Best wishes,

Mike
The greatest viral marketing idea of all time, get your copy of this Free Report now!
----
Linux Tips, Trick and Advice -- The Linux and Unix Menagerie
0

#3 User is offline   UnIxBug 

  • Newbie (User Level: 0/10)
  • Group: Members
  • Posts: 2
  • Joined: 24-February 09

Posted 25 February 2009 - 12:04 PM

In the end puting the field with month between simple quotes aka '$6' transforms it into numerical :blink:

ls -lrt /var/log * >& 1 | egrep "log" | sort -m | awk '{print strftime("%y %m %d")"\t", "/var/log/"$9"\t",
substr($8,3,4)"\t", '$6' "\t", $7}'

0

#4 User is offline   eggi 

  • User Level: 6/10
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 311
  • Joined: 25-November 07
  • Location:Grayslake, IL USA

Posted 26 February 2009 - 04:04 AM

nteresting - not what I would have expected - cool find :)

, Mike

View PostUnIxBug, on Feb 25 2009, 06:04 AM, said:

In the end puting the field with month between simple quotes aka '$6' transforms it into numerical :blink:

ls -lrt /var/log * >& 1 | egrep "log" | sort -m | awk '{print strftime("%y %m %d")"\t", "/var/log/"$9"\t",
substr($8,3,4)"\t", '$6' "\t", $7}'

The greatest viral marketing idea of all time, get your copy of this Free Report now!
----
Linux Tips, Trick and Advice -- The Linux and Unix Menagerie
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users