Substituting strings (with newlines) in many files 
Author Message
 Substituting strings (with newlines) in many files

Hello, I'd like to use

perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' FILELIST

to replace strings that contain newlines, say I have files
that look like

1
2
3

and I want to make them look like

4
5
6

I'd like to be able to do something like

perl -pi.bak -e 's/1.*3/4\n5\n6/g' *

But that doesn't work without .* being able to match newlines.  I
think this is accomplished by setting $* to a nonzero value, but
I'm not sure how to do that on the command line.  Multiple -e's
don't seem to work.

Any suggestions?

Thanks,



Tue, 28 Apr 1998 03:00:00 GMT  
 Substituting strings (with newlines) in many files
 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc,

:Hello, I'd like to use
:
:perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' FILELIST
:
:to replace strings that contain newlines, say I have files
:that look like
:
:1
:2
:3
:
:and I want to make them look like
:
:4
:5
:6
:
:I'd like to be able to do something like
:
:perl -pi.bak -e 's/1.*3/4\n5\n6/g' *
:
:But that doesn't work without .* being able to match newlines.  I
:think this is accomplished by setting $* to a nonzero value, but
:I'm not sure how to do that on the command line.  Multiple -e's
:don't seem to work.
:
:Any suggestions?

Learn about

    perl -00

and the

    s/foo/bar/s
    s/foo/bar/m
    s/foo/bar/sm

flags.

--tom
--

:       I've tried (in vi) "g/[a-z]\n[a-z]/s//_/"...but that doesn't
: cut it.  Any ideas?  (I take it that it may be a two-pass sort of solution).



Wed, 29 Apr 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Substituting newlines for HTML equivalent

2. Substituting many sparsely distrbuted strings in many files

3. how to substitute strings in a newly created file

4. Substituting $variable strings in a file

5. substituting strings in many files

6. Substituting Strings in Many Files

7. split and substitute, substitute, substitute

8. Substituting many strings in one pass over the input

9. substituting strings in source trees?

10. Pattern Match - substitute a string after the match

11. Repeat string matching/substituting ?

12. insert newlines in a long string

 

 
Powered by phpBB® Forum Software