Pass awk field to a command line executed within 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.

Pass awk field to a command line executed within awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-15-2013
tuxer tuxer is offline
Registered User
 
Join Date: Oct 2013
Last Activity: 22 December 2013, 6:03 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Pass awk field to a command line executed within awk

Hi,

I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date).
All my attempts failed this far.

Here's an example.
It works fine with timestamp hard-codded into the command

Code:
echo "1381653229 something" |awk 'BEGIN{cmd="date -d @1381653229 +\"%m/%e/%y %H:%M\""; cmd|getline; close(cmd); date=$0;}{print date"|"$2}'

How do I do something like

Code:
echo "1381653229 something" |awk 'BEGIN{cmd="date -d @$1 +\"%m/%e/%y %H:%M\""; cmd|getline; close(cmd); date=$0;}{print date"|"$2}'

Thanks in advance.
Sponsored Links
    #2  
Old 10-15-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 15 September 2014, 12:16 PM EDT
Location: India
Posts: 1,825
Thanks: 24
Thanked 524 Times in 503 Posts
You could do this, without troubling awk:


Code:
[user@host ~]$ set 1381653229 something
[user@host ~]$ echo "$(date -d @$1 +"%m/%e/%y %H:%M")|$2"
10/13/13 14:03|something
[user@host ~]$

Sponsored Links
    #3  
Old 10-15-2013
tuxer tuxer is offline
Registered User
 
Join Date: Oct 2013
Last Activity: 22 December 2013, 6:03 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Well, that was just an example.
Actually I need to run with a script on multiple files and fish designated fields out of them.
So, awk seems to be the tool.
    #4  
Old 10-15-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 26 June 2014, 10:29 AM EDT
Location: France
Posts: 2,929
Thanks: 80
Thanked 624 Times in 596 Posts

Code:
$echo "1381653229 something" | while read a b;do echo $(date -d@$a +'%D %T')"|$b" ;done
10/13/13 10:33:49|something
$

Sponsored Links
    #5  
Old 10-15-2013
tuxer tuxer is offline
Registered User
 
Join Date: Oct 2013
Last Activity: 22 December 2013, 6:03 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
balajesuri, ctsgnb

Thank you both.
After all did it your way.

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
Cannot pass rsh and awk command into a variable elcounto Shell Programming and Scripting 3 01-09-2013 03:11 PM
Pass Parameters to awk command Amrutha24 Shell Programming and Scripting 5 01-07-2013 01:40 AM
How to pass a field from awk in a pipe? Kingsley Shell Programming and Scripting 9 08-17-2010 09:00 AM
Pass command line arguments to awk kristinu Shell Programming and Scripting 2 12-15-2009 10:31 AM
can I pass awk variable to system command? zhynxn Shell Programming and Scripting 2 07-05-2006 11:11 AM



All times are GMT -4. The time now is 10:54 PM.