The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
named pipes kanchan_agr Shell Programming and Scripting 5 09-12-2007 05:48 AM
Print Problem in UNIX. Need to know the option to specify the print paper size ukarthik HP-UX 1 06-07-2007 09:35 AM
Two types of pipes? blowtorch UNIX for Dummies Questions & Answers 5 10-31-2005 09:35 PM
cd using pipes Sinbad Shell Programming and Scripting 2 09-09-2004 10:05 AM
PIPEs and Named PIPEs (FIFO) Buffer size Jus Filesystems, Disks and Memory 1 08-20-2004 10:14 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-19-2008
Jonny2Vests Jonny2Vests is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 12
awk pipes & print

Hi,

I need to awk some data from some text files, basic stuff, eg:

awk '/phrase/ {print $1,$2,$3}' file

Which will print columns 1 to 3 of all lines containing "phrase"

But what if I wanted to pipe just one of the columns to a command (in my case a date converter) as in:

awk '/phrase/ {print $1 ¦ "command" ,$2,$3}' file

The above does not work. Does anyone know what does? I'm using ksh.

Cheers, Jon.
  #2 (permalink)  
Old 02-19-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Your date converter has to be able to read from stdin. Your syntax is mostly correct. use getline to "retreive" your response on the other side of the pipe.
Code:
awk '/phrase/ {print $1 ¦ "command" | getline mydate; print mydate,$2,$3}' file
Test your date converter on the command line:
Code:
echo "$mydate" | date_converter
  #3 (permalink)  
Old 02-19-2008
ynixon ynixon is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 57
try this

Code:
awk '/phrase/ {print $1,$2,$3 ; system("command "$1)}' file
  #4 (permalink)  
Old 02-19-2008
Jonny2Vests Jonny2Vests is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 12
Quote:
Originally Posted by ynixon View Post
try this

Code:
awk '/phrase/ {print $1,$2,$3 ; system("command "$1)}' file
Thanks guys and thanks for this ynixon, which is quite elegant and what I was looking for. I'm using:

awk '/phrase/ {system("command "$1" ");print $3" "$4}' file > output

...the only problem is the ; forces a new line. Any way around that?

Jon
  #5 (permalink)  
Old 02-19-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
Here is another example using gawk
Code:
/phase/ {
      tmp = "date -r "$1;
      tmp | getline epoch;
      close(date);
      print epoch, $2, $3;
}
Sample file
Code:
2000  2 3 phase
90000 5 6 phase
7000  8 9
Output
Code:
Wed Dec 31 19:33:20 EST 1969 2 3
Thu Jan  1 20:00:00 EST 1970 5 6
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0