awk to output cmd result


 
Thread Tools Search this Thread
Operating Systems HP-UX awk to output cmd result
# 1  
Old 04-27-2008
awk to output cmd result

I was wondering if it was possible to tell awk to print the output of a command in the print.

Code:
 .... | awk '{print $0}'

I would like it to print the date right before $0, so something like (this doesn't work though)

Code:
 .... | awk '{print date $0}'

# 2  
Old 04-27-2008
Code:
... | awk '{ "date" | getline date; print date " " $0 }'

Querying the date again and again for each line is pretty wasteful, though. Perhaps you want to do it in a BEGIN block, or something like

Code:
... | awk -v date="`date`" '{ print date " " $0 }'

Really old classical awk might not have all the required facilities; try nawk or mawk or gawk if your plain awk won't do. On HP-UX in particular, you may find that the xpg4 awk is more modern than the plain /usr/bin/awk. Searching this forum suggests that setting the environment variable UNIX95=1 might get you that.

You have very clueful questions, keep it up! (-:

Last edited by era; 04-27-2008 at 09:37 AM.. Reason: UNIX95=1 tip
# 3  
Old 04-27-2008
That last one you posted works, but its static. The output of date never changes. Is there a way to make it dynamically call date every time it prints?

Thanks
# 4  
Old 04-27-2008
Avoiding that was kind of the point. Try to find an awk which is new enough to do the first.
# 5  
Old 04-27-2008
Here is one way of doing what you want:

Code:
{
   "date +%T" | getline cur_date; print cur_date, $0
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use bash command on awk field and output the result

Hello, I want to run a field from an awk command through a command in bash. For example my input file is 1,2,3 20,30,40 60,70,80 I want tot run $2 thought the command date +%d/%m/%y -d"01/01/15 + $2 days -1 day" and get the output 1,02/01/15,3 20,30/01/15,40 60,11/03/15,80 ... (2 Replies)
Discussion started by: garethsays
2 Replies

2. Shell Programming and Scripting

Different cmd to execute and based on some pattern parse it and then store result in xlx format

Hi I have a different requirement, I need to run some application on my device from a file app_name.txt one by one which is like this: /usr/apps/email /usr/apps/message /usr/apps/settings after each app while it is running I need to execute again one cmd like ps -ef |grep... (2 Replies)
Discussion started by: Sanjeev Roy
2 Replies

3. Shell Programming and Scripting

Selecting awk output depending on grep result

Hi, I don't script often enough to know how to do this, and I can't seem to find a right example online. I have a csv output from an old, old system (Win2K???), from which I want to extract only certain fields. Initially I came up with something like this: cat file1 | awk -F '"' '{print $8... (7 Replies)
Discussion started by: takada
7 Replies

4. Shell Programming and Scripting

Problem assigning cmd output to variable then using in IF statement

Hi, I'm using the bourn shell on a Sun Solaris Unix system. I am relatively new to UNIX scripting so please bear with me... I'm having a couple issues: 1) I need to have a variable $FSIZE set with the output of a command each time the script runs. (the command looks for a file and... (8 Replies)
Discussion started by: dqrgk0
8 Replies

5. Shell Programming and Scripting

set output of linux cmd as a variable in .exp

I am trying to make a script to take commands from a .txt file ( line by line) and pass it using send ( used in another function ) what i am trying to achieve is : set nol "`grep '' ${exp_path2}/cmdlist.txt | wc -l `" as in shell script nol=`grep '' $exp_path2/cmdlist.txt | wc -l` ... (0 Replies)
Discussion started by: dixyantar
0 Replies

6. Shell Programming and Scripting

sed command works from cmd line to standard output but will not write to file

Hi all .... vexing problem here ... I am using sed to replace some special characters in a .txt file: sed -e 's/_<ED>_/_355_/g;s/_<F3>_/_363_/g;s/_<E1>_/_341_/g' filename.txt This command replaces <ED> with í , <F3> with ó and <E1> with á. When I run the command to standard output, it works... (1 Reply)
Discussion started by: crumplecrap
1 Replies

7. Shell Programming and Scripting

Capturing awk's system(cmd) output

Hi everybody, I am working on a bigger awk script in which one part is comparing the size of two files. I want to evaluate which file is bigger and then just save the bigger one. I got it all working except for the part where I want to figure out which file is bigger; the one awk is currently... (2 Replies)
Discussion started by: iMeal
2 Replies

8. Programming

Storing the output of a Unix cmd in my C program

Hello experts, How can I retrieve the output from a Unix command and use it as string variable in my C program? For example, when I issue the command 'date' I get: Tue Jun 11 09:54:16 EEST 2009 I do not want to redirect the output of the command to a file and then open the file from... (3 Replies)
Discussion started by: Goseib
3 Replies

9. Shell Programming and Scripting

ksh: cmd output to input of another script

I want to write a script in KSH that takes the output of one command and redisplays it. Something like: while true do read inpt date +"%I:%M:%S %p <-> $inpt" done and then some how get the output of the ping command to redirect to the input of this script. does that make sense? (2 Replies)
Discussion started by: IMTheNachoMan
2 Replies

10. UNIX and Linux Applications

How to get a result of a cmd into variable?

Hi , I am trying following . I need to get a result of an autosys cmd into a unix variable. The autosys cmd is autostatus -G jpm_day_today Please help me in storing the value returned by this cmd into a unix variable. Appreciate your time to read this post. (1 Reply)
Discussion started by: manchau
1 Replies
Login or Register to Ask a Question