Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Help in using date command inside awk

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-21-2013
ddspark ddspark is offline
Registered User
 
Join Date: May 2011
Last Activity: 22 June 2013, 3:21 AM EDT
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
Help in using date command inside awk

Hi All,




Code:
bash-3.2$ autorep -J BOX_NAME% -l0 | grep BOX_NAME| awk -f awkScript.awk

sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file
        BOX_NAME SU 06/21/2013 03:44:03 06/21/2013 07:46:37 0

#My awkfile 
{               
       "date +%s -d \""$2" "$3"\"" | getline startTime;
       "date +%s -d \""$4" "$5"\"" | getline endTime;
       duration = (endTime - startTime)/60;
printf "\t" "%s %s %s %s %s %s %s\n",$1,$6,$2,$3,$4,$5,duration
}

when I hardcode the values in places of field variables inside date command , that works.

So basically my question is how should I use field variables like $1,$2 etc in date command in awk , I should I use quotes with them.

I tried finding on google but could not get answer .


Thanks.
Spark.
Sponsored Links
    #2  
Old Unix and Linux 06-21-2013
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 31 July 2015, 9:18 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,312
Thanks: 135
Thanked 469 Times in 423 Posts
You could use system() for example.

Code:
$ echo "+%w" | awk '{system("date " $1)}'
5

Sponsored Links
    #3  
Old Unix and Linux 06-21-2013
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 April 2015, 3:05 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 626 Times in 584 Posts

Code:
$ awk '{ ("date +%s -d " q $1 s $2 s $3 q )| getline dt; print dt }' q='"' s=' ' <<<'1 day ago'
1371735965
$ awk '{ ("date +%s -d " q $1 s $2 s $3 q )| getline dt; print dt }' q='"' s=' ' <<<'10 day ago'
1370958368

Try using the following awk script:


Code:
BEGIN {
  s = " "; q = "'"
  cmd = "date +%s -d "
  }
{
  args = q $2 s $3 q
  (cmd args) | getline startTime
  close(cmd args)
  args = q $4 s $5 q
  (cmd args) | getline endTime
  close(cmd args)
  duration = (endTime - startTime)/60
  printf "\t%s %s %s %s %s %s %s\n", $1, $6, $2, $3, $4, $5, duration
}


Last edited by radoulov; 06-21-2013 at 10:00 AM.. Reason: Corrected.
The Following User Says Thank You to radoulov For This Useful Post:
ddspark (06-22-2013)
    #4  
Old Unix and Linux 06-22-2013
ddspark ddspark is offline
Registered User
 
Join Date: May 2011
Last Activity: 22 June 2013, 3:21 AM EDT
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
Thanks radoulov .
This is working like a charm.


Spark.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Multiple command execution inside awk command during xml parsing vivek d r Shell Programming and Scripting 2 02-11-2013 12:56 AM
How to use same variable value inside as well as outside of the awk command? Ganesh Khandare Shell Programming and Scripting 12 10-05-2010 08:29 AM
running bash command inside awk ydk Shell Programming and Scripting 5 06-24-2009 09:17 AM
case command inside awk/nawk abdulaziz Shell Programming and Scripting 9 03-09-2009 09:15 PM
Running command inside awk Raghuram.P Shell Programming and Scripting 1 08-09-2007 05:54 AM



All times are GMT -4. The time now is 04:17 AM.