The UNIX and Linux Forums  

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
awk and sed filtering invinzin21 Shell Programming and Scripting 2 01-11-2008 03:56 AM
port filtering ujjwalmohan HP-UX 0 11-26-2007 01:18 AM
awk filtering ? varungupta UNIX for Advanced & Expert Users 4 09-17-2007 03:55 AM
process filtering pradeepmacha Shell Programming and Scripting 6 04-11-2005 11:18 AM
gcc version filtering !! amol Shell Programming and Scripting 4 06-28-2002 02:30 AM

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

Join Date: Aug 2008
Posts: 5
Pipe filtering

Hi,

The thing is that sometimes I need to filter an arbitrary output given a condition (think on the "filter" function present on some programming languages) from the shell (I use sh/bash). I tried this:


Code:
#!/bin/sh
# Usage: filter command [options_for_xargs]
COMMAND=$1
shift 1
xargs "$@" -I "{}" sh -c "if $COMMAND; then echo {}; fi"

And now, if I want to get all the non-executable files under /etc, I can run this:


Code:
$ find /etc -type f -print0 | filter "! test -x {}" -0
/etc/fam.conf
/etc/cups/cupsd.conf
/etc/cups/snmp.conf
/etc/cups/lpoptions
/etc/cups/mime.types
....

So the question is... is there any better/faster way of doing this generic filter with sh/bash?

thanks!
  #2 (permalink)  
Old 08-02-2008
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,814
FWIW: find supports the -mode option - you use this to test permissions bits.

Your generic filter invokes a whole new process, which may not be all that efficient when compared to other options available to a command. It is an interesting idea, though. Have you considered the concept of a coprocess using named pipes?

See the bash FAQ here:
ftp://ftp.cwru.edu/pub/bash/FAQ
  #3 (permalink)  
Old 08-03-2008
tokland tokland is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
Quote:
Originally Posted by jim mcnamara View Post
FWIW: find supports the -mode option - you use this to test permissions bits.
Yes, I'm aware of this, it was just an example.

Quote:
Originally Posted by jim mcnamara View Post
Your generic filter invokes a whole new process, which may not be all that efficient when compared to other options available to a command.
Certaintly. A pure bash solution (less versatile as it does not use xargs) I though was as simple as:


Code:
#!/bin/sh
COMMAND=$1
while read LINE; do
     eval "${COMMAND/\{\}/$LINE}" && echo $LINE
done

Quote:
Originally Posted by jim mcnamara View Post
Have you considered the concept of a coprocess using named pipes?
No, I didn't, how could it be written? anyway you gave me an idea: taking the 'sh' out of xargs and run it on a pipe:


Code:
xargs "$@" -I "{}" echo "$COMMAND && echo {}" | sh

It's not as fast as the pure bash solution, but is cheaper to run "echo"s than "sh"s processes. If it was possible to for xargs to output the command without actually running it, it should really fast (there is no such option AFAIK)

thanks
  #4 (permalink)  
Old 08-03-2008
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,814
The FAQ shows you how to write coprocesses with named pipes.
Closed Thread

Bookmarks

Tags
filter shell xargs

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 08:31 AM.


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