Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl regex multi-line substitution weirdness

Status
Not open for further replies.

acutchin

Technical User
Oct 16, 2001
6
US
I am trying to do a simple multi-line substitution in perl, but encountering intractable weirdness. How in the world do I replace BOTH of the lines with "FOOBAR"? The file does not appear to have any windows/dos-style line breaks or CR characters.

# ls -l fb
-rw------- 1 aaronc devel 8 Nov 3 11:28 fb
# cat fb
foo
bar
# cat fb | perl -nle '$_ =~ s/foo/FOO/; print $_'
FOO
bar
# cat fb | perl -nle '$_ =~ s/foo\nbar/FOOBAR/m; print $_'
foo
bar
# cat fb | perl -nle '$_ =~ s/foo$bar/FOOBAR/m; print $_'
FOOBAR
bar
# cat fb | perl -nle '$_ =~ s/^foo$bar$/FOOBAR/m; print $_'
FOOBAR
bar
# cat fb | perl -nle '$_ =~ s/^foo$^bar$/FOOBAR/m; print $_'
foo
bar
# cat fb | perl -nle '$_ =~ s/^foo\nbar$/FOOBAR/m; print $_'
foo
bar
# cat fb | perl -nle '$_ =~ s/^foo\nbar$/FOOBAR/mg; print $_'
foo
bar
# cat fb | perl -nle '$_ =~ s/foo\nbar/FOOBAR/mg; print $_'
foo
bar
# cat fb | perl -nle '$_ =~ s/^foo$\n^bar$/FOOBAR/m; print $_'
foo
bar
# cat fb | perl -nle '$_ =~ s/^foo$\n^bar$/FOOBAR/mg; print $_'
foo
bar

-- Aaron
 
I suggest that you ask specifically what you're trying to do. Here is a regex that will do what you ask, but it's probably catching a lot more than what you want. Ask a specific problem, or just continue reading up on regex's. Good luck.

Code:
cat fb | perl -nle 's/^\w+/FOOBAR/g; print'
 
Thank you MillerH. Maybe this is more clear:

# cat file
one
two
three
four

after parsing, i want the file to read:

# cat file
one
FOOBAR
four

how do i do that?

-- Aaron
 
Your problem arises in the use of the -n parameter. This makes your code synonymous with the following:

Code:
while (<>) {
    # Substitution code here.
    s/^\w+/FOOBAR/g;
    # Print code here
    print;
}

That code process through each line of the file one at a time, instead of as one big chuck. Because of this, the regular expression never sees beyond each individual line.

I never run perl from the command line like you're doing, so I don't yet know the proper way to get this to work. Try this example to confirm what I'm telling you though.

Code:
cat fb | perl -nle "s/./FOOBAR/; print"

I originally believed that the regex would only run once, and would replace the first character of the file with FOOBAR. Instead it ran 4 times and replaced the first character on every line.

Code:
FOOBARne
FOOBARwo
FOOBARhree
FOOBARour

There should be a way to get the regex to parse the entire file in one pass, but I don't know what it is yet. I'll look into it more this afternoon, but maybe someone else already has experience doing this type of operation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top