http://www.perlmonks.org?node_id=849319


in reply to Re: Join lines that match an string
in thread Join lines that match an string

Kennethk, Linuxer, Ambrus, thanks a lot for your help, Linuxer you got the idea perfectly, many times is very dificult to explain yourself in words and you did it.

The best solution is what Ambrus wrote

perl -wpe 'if (/\bremotely\b/.../\bp_args\b/) { chomp }'

The only problem with this is that it joins the line that is after "p_args" but I can deal with that (perl -e "s/\';host=/\';\host=\n/g" -p data00 > data01. Thanks a lot again for your help, tips and advice

Replies are listed 'Best First'.
Re^3: Join lines that match an string
by ambrus (Abbot) on Jul 13, 2010 at 17:35 UTC

    Right, unlike the other solutions, that one joins the line with the terminator and the next one. You could write

    perl -wpe 'if ((/\bremotely\b/.../\bp_args\b/) !~ /^$|E0/) { chomp }' +a
    but that's a bit ugly, or decide that the flip-flop shortcut isn't appropriate in such case and track the flag by hand, such as
    perl -wpe 'if (($_f ||= /\bremotely\b/) &&= !/\bp_args\b/) { chomp }' +a

      Linuxer your code is great as Ambrus one and yours keep the last line where it shoudl be, just to end this, how could I insert a blank where there was a line feed (\n)? because in some cases I need to separate the info and in others no

      Again thanks for your help

        Read Re: Join lines that match an string again, for it has two examples that both don't join the line after the p_args line, and do add a space if there'd be none where the lines are joined.

Re^3: Join lines that match an string
by linuxer (Curate) on Jul 13, 2010 at 17:21 UTC

    But I still have the feeling I didn't get the whole idea correctly; especially when I see ambrus' solution.

    If you want to keep the linebreak in the "p_agrs" line, then tell perl not to chomp it.

    Example:

    perl -wpe 'if ( m/\bremotely\b/ ... m/\bp_agrs\b/ ) { chomp unless m/\bp_agrs\b/ }'