Pass awk field to a command line executed within awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass awk field to a command line executed within awk
# 1  
Old 10-15-2013
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.
# 2  
Old 10-15-2013
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 ~]$

# 3  
Old 10-15-2013
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
Code:
$echo "1381653229 something" | while read a b;do echo $(date -d@$a +'%D %T')"|$b" ;done
10/13/13 10:33:49|something
$

# 5  
Old 10-15-2013
balajesuri, ctsgnb

Thank you both.
After all did it your way.

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

awk command not getting executed in shell script

I am able to execute awk command from shell prompt. but the same command is not getting executed when written and run in a bash script the command from bash cmd prompt. awk '/world/{for (i=2; i<NF; i++) printf $i " "; print $NF}1' myfile >tmp$$ ; mv tmp$$ myfile file: # hello world my... (4 Replies)
Discussion started by: ashima jain
4 Replies

3. Shell Programming and Scripting

How to pass command line arguments to awk program?

#!/bin/awk -f BEGIN { FS=":"; } { if ( $7 == "" ) { print $1 ": no password!"; } } I want to execute this program for a particular user to check for his password from the file /etc/passwd (as the input file) and the user details to be given... (1 Reply)
Discussion started by: sri.phani
1 Replies

4. Shell Programming and Scripting

How to pass filename as arguement to awk command?

Hi, I am facing one issue. The awk command works fine if i hardcode the file name but if is pass it as an arguement it doesn't work. For e.g:Below commands works fine awk -v A="$type" '{F=substr($0,23,8) "_LTD_" A ".txt"; print $0 >> F; close(F) }' RL004.txt But the below command does not... (2 Replies)
Discussion started by: Neelkanth
2 Replies

5. Shell Programming and Scripting

Cannot pass rsh and awk command into a variable

Hello, I'm having some issues getting a home dir from a remote server passed to a variable. Here is what I have so far: rsh server "(ls -ld /home*/user | awk '{print \$9}')" /home3/userThat works fine and brings back what I need. But when I try to add it to a variable it goes all... (3 Replies)
Discussion started by: elcounto
3 Replies

6. Shell Programming and Scripting

Pass Parameters to awk command

I need to pass values at runtime for the below awk command where l is the length and partial.txt is the file name. awk -v l=285 '{s="%-"l"s\n";printf(s,$0);}' partial.txt > temp1.txt; (5 Replies)
Discussion started by: Amrutha24
5 Replies

7. Shell Programming and Scripting

How to pass a field from awk in a pipe?

Thanks in advance : ) I try for a long time searching for a way to split a large gzip csv file into many gzip files (except for the last sub-file which is to joint the next big file's children.) All the subfiles are to be named by the field. But I only managed to split them into the... (9 Replies)
Discussion started by: Kingsley
9 Replies

8. Shell Programming and Scripting

Pass command line arguments to awk

I am trying to pass max as a sommand line argument when I call awk. Made the modification in the BEGIN but it is not working I'm getting an error as below: awk: txsrx.awk:82: (FILENAME=jcd.tx FNR=4161) fatal: cannot open file `40' for reading (No such file or directory) Somehow it... (2 Replies)
Discussion started by: kristinu
2 Replies

9. Shell Programming and Scripting

how to pass variable in NR function used in awk command?

Hi I want to pass variables with the NR function in awk command. test_file1 is input file having 500 records. var1=100. var2=200 awk -F" " 'NR >= $var1 && NR <= $var2' test_file1 > test_file2. My end result should be that test_file2 should have records from line number between... (2 Replies)
Discussion started by: Nishithinfy
2 Replies

10. Shell Programming and Scripting

can I pass awk variable to system command?

I wanna use a system function to deal with several data. So I use awk variable FILENAME to transfer the file directory to system command, but it does not work. I use a shell function "out_function" to deal with data and save the result in another directory with the same file name. How can I... (2 Replies)
Discussion started by: zhynxn
2 Replies
Login or Register to Ask a Question