in reply to new line every match

It prints: address 6433 main st address 6434 main st address 6435 main st It does not print this part: This is the list of address here are the addreses I want it to look exactly like this: This is the list of address here are the addreses address 6433 main st address 6434 main st address 6435 main st

Replies are listed 'Best First'.
Re^2: new line every match
by aaron_baugher (Curate) on Jul 07, 2012 at 02:58 UTC

    Then I would describe the process as:

    • for each line
      • if it starts with 'address', replace any space characters preceding the word 'address' with a newline
      • if not, pass the line through unchanged

    So that would give me this code, which you can wrap in whatever input/output you need to do once you understand it:

    while(<>){ if(/^address/){ s/ address/\naddress/g; print; } else { print; } }

    Aaron B.
    Available for small or large Perl jobs; see my home node.

Re^2: new line every match
by davido (Cardinal) on Jul 07, 2012 at 00:02 UTC

    Did my reply here fail to meet your needs? I think I understood your question. Why are you repeating it?


    Dave

      Sorry Dave, but it did fail my needs. I needed someone to help fix my code, but not totally change it.

        Well, it's "just start over" broken, but if you want to keep it the way it is and just make it work with as few changes as possible, this should do:

        print "This is the list of address\n", "here are the addresses\n"; while (<INFILE>) { my($line) =$_; chomp($line); while ($line =~ /(address............)/g ) { my @values = split(' ', $line); foreach my $val (@values) { next; print "$val\n"; } print "$1\n"; } }

        You'll note I added two print statements, a next, changed an if( to a while(, and added a /g to your regex. The rest is untouched. If your input data is what you say it is in your original post (a single line of text containing multiple address fields), this works fine and lets you feel like the original code didn't change much.

        If it doesn't work, go back and verify that your input data looks exactly like what you posted in your original question.


        Dave