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.
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.
awk '/phrase/ {print $1 � "command" | getline mydate; print mydate,$2,$3}' file
Test your date converter on the command line:
echo "$mydate" | date_converter
ynixon
February 19, 2008, 5:00pm
3
try this
awk '/phrase/ {print $1,$2,$3 ; system("command "$1)}' file
Here is another example using gawk
/phase/ {
tmp = "date -r "$1;
tmp | getline epoch;
close(date);
print epoch, $2, $3;
}
Sample file
2000 2 3 phase
90000 5 6 phase
7000 8 9
Output
Wed Dec 31 19:33:20 EST 1969 2 3
Thu Jan 1 20:00:00 EST 1970 5 6
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