[OT] writing filters in sh

Devin Teske dteske at vicor.com
Thu Oct 28 18:08:18 UTC 2010


On Thu, 2010-10-28 at 09:17 -0700, Chip Camden wrote:
> Quoth Chad Perrin on Thursday, 28 October 2010:
> > On Thu, Oct 28, 2010 at 08:11:48AM -0700, Chip Camden wrote:
> > > 
> > > Here's a way to do what you're wanting to do.  Unfortunately, it isn't a
> > > generalized, single construct:
> > > 
> > > #!/bin/sh
> > > if [ $# -ge 1 ];then
> > >   exec cat $@ | $0
> > >   exit
> > > fi
> > > 
> > > while read data; do
> > >   echo $data
> > > done
> > > 
> > > My lame attempts to generalize the first paragraph into an alias,
> > > function, or shell script have met with disappointment.

Here you go:

alias myfilter="/bin/sh -c '"'myfilter() { if [ $# -gt 0 ]; then while
[ $# -gt 0 ]; do [ -f "$1" ] && myfilter < "$1"; shift 1; done; else
while read LINE; do : some transform ; echo $LINE; done; fi; }; myfilter
"$@"'"' -- /bin/sh"

The above is to be entered as a single command-line.

NOTE: If you're using either csh or tcsh, you only need to make one
change... change "alias myfilter=..." to instead "alias
myfilter ..." (that's right, just change the equals sign to a space).

Here's a demo:

$ printf "Line 1\nLine 2\nLine 3\n" | myfilter
Line 1
Line 2
Line 3

$ head -2 /etc/rc.conf | myfilter
# This file now contains just the overrides from /etc/defaults/rc.conf
# please make all changes to this file.

$ myfilter < /etc/rc.conf | head -2
# This file now contains just the overrides from /etc/defaults/rc.conf
# please make all changes to this file.

$ cat /etc/rc.conf | myfilter | head -2
# This file now contains just the overrides from /etc/defaults/rc.conf
# please make all changes to this file.

$ myfilter > somefile
Line 1
Line 2
Line 3
^D
$ cat somefile
Line 1
Line 2
Line 3



> > 
> > I was hoping for a generalized, simple idiom for this, rather than
> > needing to implement it myself, for demonstration purposes (and for easy
> > reuse later, of course).  Your solution does not exactly fit my
> > preferences for simplicity, but I might include it in an article I'm
> > writing anyway.  It's simple and readable enough that it should not
> > clutter up the article much.
> > 
> > I tested it for some simple use cases, and it works well.  Thanks for
> > saving me a little trouble.
> > 
> > -- 
> > Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]
> 
> 
> You're most welcome.  Perhaps someone with more sh fu can transform the
> 'if' paragraph into a one-liner at least.  When I tried to do so, I got an
> unexpected ; error.
> 



-- 
Cheers,
Devin Teske

-> CONTACT INFORMATION <-
Business Solutions Consultant II
FIS - fisglobal.com
510-735-5650 Mobile
510-621-2038 Office
510-621-2020 Office Fax
909-477-4578 Home/Fax
devin.teske at fisglobal.com

-> LEGAL DISCLAIMER <-
This message  contains confidential  and proprietary  information
of the sender,  and is intended only for the person(s) to whom it
is addressed. Any use, distribution, copying or disclosure by any
other person  is strictly prohibited.  If you have  received this
message in error,  please notify  the e-mail sender  immediately,
and delete the original message without making a copy.

-> END TRANSMISSION <-



More information about the freebsd-questions mailing list