FW: [Gllug] Awk Sed

Kieran Barry kieran at esperi.demon.co.uk
Fri Dec 7 10:28:44 UTC 2001


On Fri, 7 Dec 2001, harry wrote:

> On Thursday 06 Dec 2001 7:50 pm, Alex wrote:
> >  Perl -p -i'.orig' -e 's/(Alex)/$1andria/g;' *
> 
> I did not entirely understand what you have done there as I am no Perl guru
> but  another technique I found while playing with sed is this. 

Not that I understand a great deal about perl but taking the above line: 

> > Perl -p -i'.orig' -e 's/(Alex)/$1andria/g;' *
                            ^    ^
The data within the marked brackets is stored as $1 within the regular
expression.  This feature exists in sed also (but you need to type
s/\(Alex\)/.... and replace $1 with \1 for it to work).
> 
> Put the following in a shell script
> #! /bin/sh
> sed -e 's/'$2'/'$3'/g' $1 > $1ff; more $1ff > $1; rm $1ff; rm *~
> 
I was rather confused about the rm * here.  I misparsed "~" as some kind
of quote...

> Pass it something like
> >$ ./harrysed textfile house maison
> or
> >$./harrysed sedscript.sh 's\/hte\/the\/'  's\/hte\/the\/g'
> 
This seems to work.  I'd be interested on style comments from real
gurus...
> 
> This is by nomeans a great way of doing it but it does seem to work for 
> individual files. I will not be using it until I hear from the list first 
> though as I am sure its just an accident waiting to happen. I think it was 
> Formi who said about not being able to replace text in a file but I have 
> tried this and it seems to work for single files but this does not solve my 
> multiple file problem. I think the above could be modified to run through 
> multiple files in some manner but it is 02:40 and my head hurts.
> 
for file in foo*; do ./harrysed sedscript.sh 's\/hte\/the\/'
's\/hte\/the\/g'; done

or 

for file in `find . -print | xargs file | grep text | sed 's/:.*//'`; do
...;done

> I need to learn more Perl as it keeps coming up as the best utility for the
> job on more occasions than I care to remember.

The correct technology for the task in hand is to use regular
expressions. (These are the s/...// things)

Regexps are available in perl, sed, vi, emacs, python and even Java.
I think a variant is available in awk.  Unfortunately, the syntax is
likely to differ between tools.

Perl has rather powerful regexps with one of the more simple syntaxs
(read: a minimum of backslashes).  And you can use it for rather more
than just the example to hand.  In general, the correct tool is the one
you know, though.

Regards

Kieran


-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list