Re: Awk, filtering match through external command

2012-12-28 Thread Vilius Panevėžys
Provided you can rely on gawk extensions, a coprocess [1] should help.

 
[1]
https://www.gnu.org/software/gawk/manual/gawk.html#Getline_002fCoprocess


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/20121228161728.6e6c1611@debws



Re: Awk, filtering match through external command

2012-11-21 Thread Chris Davies
Neal Murphy neal.p.mur...@alum.wpi.edu wrote:
 I don't see a way to pipe both ends of an external command. I'm not sure 
 even *perl* could do that.

It can [*], see IPC::Open2 and IPC::Open3. But you can end up with all
sorts of buffer related race conditions.

Chris
-- 
[*] are you really surprised?


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/gfbvn9xn65@news.roaima.co.uk



Re: Awk, filtering match through external command

2012-11-10 Thread songbird
T o n g wrote:
...
 Any way to filter through external command to variable, somewhat like:

  head /etc/group | awk '{print $0 | cut -d':' -f1 | getline result ;}'


  looks like you want to assign the output of command to
a variable.

  in shell or bash use the backquote character `

result=`head /etc/group | cut -d: -f1`

example run:

me@ant(16)~$ result=`head /etc/group | cut -d: -f1`
me@ant(17)~$ echo $result
root daemon bin sys adm tty disk lp mail news


  songbird


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/6l52n9-eij@id-306963.user.uni-berlin.de



Re: Awk, filtering match through external command

2012-11-10 Thread T o n g
On Sat, 10 Nov 2012 00:36:48 -0500, John L. Cunningham wrote:

 Well...
 
 head /etc/group |\
 awk '{echo  $0  | cut -d: -f1 | getline result; print result}'
 
 ...seems to work. 

Great. Thanks. 

 But it is also very silly.

Again, as I said, this is only 
*for the sake of illustration of filtering through external command*.
I didn't want to complicate/distract the discussion on how to match a 
piece of segment on a complicated XML string then beautify it via 
external command tidy, then do further processing..., all within awk,  
make sense now? 

Thanks everyone!



-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/k7nbf4$vab$1...@ger.gmane.org



Re: Awk, filtering match through external command

2012-11-09 Thread T o n g
On Thu, 08 Nov 2012 23:29:23 -0500, John L. Cunningham wrote:

 However, I want the cmd itself a piping command as well.  E.g..,
 
  echo  substr($0, RSTART, RLENGTH) | command external with
  parameters|
 getline result
 
 I'm not getting it. Perhaps you could post a sample input with the
 desired output?

*For the sake of illustration of filtering through external command*: 

It's OK to:

 head /etc/group | awk '{print $0 | cut -d':' -f1;}'

or, 

 head /etc/group | awk '{ echo  $0 | getline result ; print result}'

Any way to filter through external command to variable, somewhat like:

 head /etc/group | awk '{print $0 | cut -d':' -f1 | getline result ;}'

Any way to make it works?

Thanks


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/k7kl5d$bko$1...@ger.gmane.org



Re: Awk, filtering match through external command

2012-11-09 Thread John L. Cunningham
On Sat, Nov 10, 2012 at 04:28:29AM +, T o n g wrote:
 
 It's OK to:
 
  head /etc/group | awk '{print $0 | cut -d':' -f1;}'

Why not:

head /etc/group | awk -F: '{print $1}'

 
 or, 
 
  head /etc/group | awk '{ echo  $0 | getline result ; print result}'
 
 Any way to filter through external command to variable, somewhat like:
 
  head /etc/group | awk '{print $0 | cut -d':' -f1 | getline result ;}'
 
 Any way to make it works?

Well...

head /etc/group |\
awk '{echo  $0  | cut -d: -f1 | getline result; print result}'

...seems to work. But it is also very silly.

-- 
John


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/20121110053647.ga27...@cerulean.myhome.westell.com



Re: Awk, filtering match through external command

2012-11-09 Thread Neal Murphy
On Friday, November 09, 2012 11:28:29 PM T o n g wrote:
 Any way to filter through external command to variable, somewhat like:
 
  head /etc/group | awk '{print $0 | cut -d':' -f1 | getline result ;}'
 
 Any way to make it works?

You'd *think* there'd be a way to do that, but I don't think awk works that 
way. I don't see a way to pipe both ends of an external command. I'm not sure 
even *perl* could do that.

Your best bet is to follow the ancient UNIX mantra of writing a program or 
script to do one thing well. Split the awk script into two parts, as in:
  awk -f part1  file | external_filter | awk -f part2

Of course, if it's a simple filter like the one you illustrated, you can do it 
in awk:
  awk '{split($0, result, :); print result[1];}' /etc/group
or
  awk '{sub(/:.*/, , $0); print;}'  /etc/group


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/201211100122.30219.neal.p.mur...@alum.wpi.edu



Re: Awk, filtering match through external command

2012-11-08 Thread John L. Cunningham
On Fri, Nov 09, 2012 at 03:47:15AM +, T o n g wrote:
 Hi, 
 
 Awk allows reading results from external command:
 
  cmd | getline result

Over in comp.lang.awk they will tell you to avoid using getline if at
all possible because it changes the way your program operates in ways
that are difficult to anticipate.

 
 However, I want the cmd itself a piping command as well.  E.g.., 
 
  echo  substr($0, RSTART, RLENGTH) | command external with
  parameters| 
 getline result
 
 Any way to make it works?
 

I'm not getting it. Perhaps you could post a sample input with the
desired output?

-- 
John


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/20121109042922.ga10...@cerulean.myhome.westell.com