[lug] using sed.

Tkil tkil at scrye.com
Mon Nov 6 11:41:08 MST 2000

>>>>> "Ralf" == rm  <rm at mamma.varadinet.de> writes:

Ralf> Sorry, i had a lot of latency on the line and somehow a 
Ralf> space crept in. The line should read:
Ralf>   perl -ip.bu -e 's/(blank)/$1.extension/og;' dir/*
Ralf>           ^  
Ralf>           |
Ralf> 	  *---- no space here!

Ralf> Thanks for the correction!

i hate to do this to you again, but i'm pretty sure that *everything*
after the "-i", to the next whitespace, is used as the backup suffix.
so "-ip.bu", on filenames file1, file2, file3, etc, should result in
files "file1p.bu", "file2p.bu", "file3p.bu"...  

but i should test this.

weird.  when i try the flags you used, nothing happened:

| $ for i in a b c d e ; do echo -e "foo\nbar\nbaz" > $i ; done
| $ cat a
| foo
| bar
| baz
| $ perl -ip.bu -e 's/foo/bar/g' a b c d e
| $ cat a
| foo
| bar
| baz
| $ ls
| a  b  c  d  e

now, using one of mine:

| $ perl -i~ -pe 's/foo/bar/g' a b c d e
| $ ls
| a  a~  b  b~  c  c~  d  d~  e  e~
| $ cat a
| bar
| bar
| baz

which is closer to what i expected.  i believe the problem with yours
is that all of "p.bu" was indeed interpreted as a backup suffix, but
since no output was generated, perl didn't bother creating a new
file.  check:

| $ perl -ip.bu -e 's/foo/bar/g; print "hi mom\n"' a b c d e
| hi mom

ha.  since it never saw "-p", it didn't iterate over all the files --
it just executed what was in the "-e" string, and quit!

anyway.  today's perl moment has been brought to you by the letter


p.s. one more microoptimization -- you don't need "/o" on a pattern
     unless the search pattern contains a variable (and even then,
     it's rarely needed, since i believe perl is smart enough to see
     when a variable has changed and cache the results of regex
     compilation until then -- but i could be wrong.)

More information about the LUG mailing list