Beefy Boxes and Bandwidth Generously Provided by pair Networks Ovid
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Join lines that match an string

by ranrodrig (Novice)
on Jul 13, 2010 at 17:02 UTC ( #849319=note: print w/ replies, xml ) Need Help??


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


Comment on Re^2: Join lines that match an string
Download Code
Re^3: Join lines that match an string
by linuxer (Deacon) 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/ }'

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://849319]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-04-16 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls