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.

How to use shell command in awk ?

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-01-2009
salman4u salman4u is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 29 March 2009, 9:08 AM EDT
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
How to use shell command in awk ?

Hello,
i was just wondering how can i use shell command in awk script file? say suppose i want to print the date at the top of the report i generate using awk how can i do that? Thanks in advance! Linux
Sponsored Links
    #2  
Old Unix and Linux 03-01-2009
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 27 July 2015, 1:09 PM EDT
Location: The Netherlands
Posts: 7,731
Thanks: 129
Thanked 547 Times in 515 Posts
Possibilities:


Code:
awk 'BEGIN{"date" | getline d; print d; close("date")}
{# Other commands....}
' file

Or:


Code:
awk -v d="$(date)" 'BEGIN{print d}
{# Other commands....}
' file

Or:


Code:
awk 'BEGIN{system("date")}
{# Other commands....}
' file

Regards
Sponsored Links
    #3  
Old Unix and Linux 03-01-2009
salman4u salman4u is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 29 March 2009, 9:08 AM EDT
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
thanks Franklin52,
the last one is the easiest for me haha! and can you please tell me how can i do the following in awk script file?


Code:
 for i in `ls`
 do
      echo $i
 done

    #4  
Old Unix and Linux 03-01-2009
salman4u salman4u is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 29 March 2009, 9:08 AM EDT
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
also why is this not working?


Code:
BEGIN{
	system("date \"+%H-%M\"")|getline d
}
{
	
}

Sponsored Links
    #5  
Old Unix and Linux 03-01-2009
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 27 July 2015, 1:09 PM EDT
Location: The Netherlands
Posts: 7,731
Thanks: 129
Thanked 547 Times in 515 Posts
Quote:
Originally Posted by salman4u View Post
thanks Franklin52,
the last one is the easiest for me haha! and can you please tell me how can i do the following in awk script file?


Code:
 for i in `ls`
 do
      echo $i
 done

Do you mean something like this?


Code:
awk 'BEGIN{
while ("ls" | getline var){
  #Do something with var...}
}
{...}'

Quote:
also why is this not working?
Code:
BEGIN{
system("date \"+%H-%M\"")|getline d
}
{

}
The system command returns the exit status of the command that was executed not the output of the command.

Regards
Sponsored Links
    #6  
Old Unix and Linux 03-02-2009
salman4u salman4u is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 29 March 2009, 9:08 AM EDT
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks again Franklin52 Linux
now things getting into my mind.
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
How to send a Xterm command via a shell command MROZ Shell Programming and Scripting 1 12-29-2008 06:48 AM
How to use more than one MPE command STREAM with Unix command in a single shell? bosskr HP-UX 1 10-16-2006 04:16 PM
How to use more than one MPE command STREAM with Unix command in a single shell? bosskr Shell Programming and Scripting 0 09-19-2006 09:44 PM
How to run a command in shell 'A' from shell 'B' rakabarp UNIX for Dummies Questions & Answers 1 09-29-2004 03:15 AM
sed command in c shell nattynatty Shell Programming and Scripting 5 11-08-2002 10:55 AM



All times are GMT -4. The time now is 11:23 AM.