Help in using date command inside awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Help in using date command inside awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 06-21-2013
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 24 October 2014, 2:41 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,237
Thanks: 121
Thanked 453 Times in 412 Posts
You could use system() for example.

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

Sponsored Links
    #3  
Old 06-21-2013
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 24 October 2014, 5:25 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 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 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 07:19 AM.