RenMcCourtey has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I just hit the issue somewhere I wouldn't expect it. Let's say I want to remove new lines from file, but only some of them. It's obvious to me how to get rid of them all, and it's also what is described everywhere. But while it's easy to replace all Us with Os and all new lines with 'bla', how do I remove new lines from lines starting with U only? I tried following without any success:

perl -pe 's/\nU/U/g' /tmp/nejms.txt perl -pe 's/^U/U/g' /tmp/nejms.txt

Replies are listed 'Best First'.
Re: Remove SOME new lines
by hippo (Archbishop) on Jun 29, 2016 at 09:28 UTC

    The -p flag treats the file one line at a time so your first regex will never match. Overcome this with -0:

    $ cat in foo bar Ubar Ubaz quux $ perl -p0e 's/\nU/U/g' in foo barUbarUbaz quux $
      Oh boy, and I thought I was looking everywhere. Thanks a lot!
Re: Remove SOME new lines
by haukex (Archbishop) on Jun 29, 2016 at 09:29 UTC

    Hi RenMcCourtey,

    Is this what you're looking for?

    $ cat in.txt AaX ObU UcU XdR ZeZ UfH UgU FhA OiG UjX UkZ UlZ xmT $ perl -pe 'chomp if /^U/' in.txt AaX ObU UcUXdR ZeZ UfHUgUFhA OiG UjXUkZUlZxmT

    Update: Explanation: When using the -p switch without the -l switch (perlrun), each line read into $_ still contains the newline, which is normally output when the lines are printed back out. So the chomp just chops the newline off, and those lines are printed without the final newline.

    Hope this helps,
    -- Hauke D

Re: Remove SOME new lines
by BrowserUk (Patriarch) on Jun 29, 2016 at 09:30 UTC

    Add /s to your substitution:

    $s = "fred\nundone\nfred\nunknown\nfred\nuncaring\n";; $t = $s; $s =~ s[\nu][u]g; print $t;; fred undone fred unknown fred uncaring $t = $s; $s =~ s[\nu][u]sg; print $t;; fredundone fredunknown freduncaring

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice. Not understood.
Re: Remove SOME new lines
by RenMcCourtey (Acolyte) on Jun 29, 2016 at 12:17 UTC
    And why this perl -p0e 's/\n^\s*$//g' nejms.txt doesn't remove blank lines? :-(

      Hi RenMcCourtey,

      Because by default ^ and $ only match at the beginning and end of the string, not each line within the string (I'm simplifying a little here - for the details see ^, $, and the /m modifier in perlre). This works:

      perl -p0e 's/\n^\s*$//mg' input.txt

      But requires you to read the entire file* into memory. I'd use:

      perl -ne 'print if /\S/' input.txt

      Hope this helps,
      -- Hauke D

      * assuming it doesn't contain NUL characters

        Oh, thank you. I guess I'll have to start completely new question, being more descriptive.