awk pipes & print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk pipes & print
# 1  
Old 02-19-2008
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  
Old 02-19-2008
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  
Old 02-19-2008
try this

Code:
awk '/phrase/ {print $1,$2,$3 ; system("command "$1)}' file

# 4  
Old 02-19-2008
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

# 5  
Old 02-19-2008
Quote:
Originally Posted by ynixon
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print & and \n while replacing with sed/awk?

string="din&esh\nisgood" File.txt: the name is sed "s#\#${string}#g" File.txt Output am getting: the name is dinesh is good Expected output: the name is din&esh\nisgood The input string is dynamic it will be keep on changing am able to handle & by placing \& in the string.. (5 Replies)
Discussion started by: dineshaila
5 Replies

2. Shell Programming and Scripting

awk to find and replace Double quotes between pipes

Hello, Need a AWK command to find and replace Double Quotes in Pipe delimited files Actully its a CSV file converted to Pipe but the double quotes still exists. So i want to Get rid of them. Example Input 1|2|3|sadsad|"Abc Efg 3"""|dada Output 1|2|3|sadsad|Abc Efg 3"|dada Thanks... (5 Replies)
Discussion started by: krux_rap
5 Replies

3. UNIX for Dummies Questions & Answers

How to create a print filter that print text & image?

Currently, I have a print filter that takes a text file, that convert it into PCL which then gets to a HP printer. This works. Now I need to embedded a image file within the text file. I'm able to convert the image file into PCL and I can cat both files together to into a single document... (1 Reply)
Discussion started by: chedlee88-1
1 Replies

4. Shell Programming and Scripting

awk question: How to print condition of NR & NF together.

Experts: LINE1 :This is line one The FIRST line of the file. LINE2 :This is line two LINE3 :This is line three with 8 fileds LINE4 :This is line four LINE5 :This is line five LINE6 :This is line six with 8 fileds I want to delete line 1, and then process the file and want to print lines... (2 Replies)
Discussion started by: rveri
2 Replies

5. Shell Programming and Scripting

awk Help: Filter Multiple Entry & print in one line.

AWK Gurus, data: srvhcm01 AZSCI srvhcm01 AZSDB srvhcm01 BZSDB srvhcm01 E2QDI31 srvhcm01 YPDCI srvhcm01 YPDDB srvhcm01 UV2FSCR srvhcm01 UV2FSBI srvhcm01 UV2FSXI srvhcm01 UV2FSUC srvhcm01 UV2FSEP srvhcm01 UV2FSRE srvhcm01 NASCI srvhcm01 NASDB srvhcm01 UV2FSSL srvhcm01 UV2FSDI (7 Replies)
Discussion started by: rveri
7 Replies

6. Programming

C++ socket, fork & pipes

Hello, I'm stuck and this is a matter which I need to resolve quite fast (but I couldn't post in the "Emergency" section); the problem is this : I have created a chat program in which the client sends the sentence to the server and then the server should send it to all the clients connected,... (2 Replies)
Discussion started by: timmyyyyy
2 Replies

7. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

8. Shell Programming and Scripting

awk modify multiple columns with pipes

Hello, I have a CSV-like dataset where some of the columns contain HTML snippets which I need to convert to XHTML. For any given snippet, I have a functioning config for the text processor 'tidy' such that tidy -config tidy.cfg example.html does the job I need done. I would like to process... (10 Replies)
Discussion started by: bstamper
10 Replies

9. Shell Programming and Scripting

Multiple pipes toward a single awk command

Hello, I would like to pipe two variables into awk, but I don't know how to do. Each variable, "a" and "b", are in fact a list of data. They are not files. So to get awk to work with it I am using: echo $a | awk 'FNR==NR{print $1}FNR!=NR{print $4}' The above works, but when I am... (5 Replies)
Discussion started by: jolecanard
5 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question