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.

Pass awk field to a command line executed within awk

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 10-15-2013
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 25 May 2015, 1:40 AM EDT
Location: India
Posts: 1,886
Thanks: 27
Thanked 539 Times in 516 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 Unix and Linux 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 Unix and Linux 10-15-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 7 April 2015, 6:37 PM EDT
Location: France
Posts: 2,931
Thanks: 81
Thanked 626 Times in 597 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 Unix and Linux 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.

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