[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Awk, filtering match through external command



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


Reply to: