Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Join lines that match an string

by ambrus (Abbot)
on Jul 13, 2010 at 09:51 UTC ( [id://849217]=note: print w/replies, xml ) Need Help??


in reply to Join lines that match an string

If you don't mind words running together where you join the lines, try this command:

echo -e 'g/\<remotely\>/.,/\<p_args\>/j\nwq' | ed -sl filename

Example input and output follow.

$ cat a "Yes, and to be grown up, my child, to venerate him," replied Glenarvan, deeply touched by the boy's genuine affection. During this conversation the horses remotely had been slackening speed, and were only walking now. "You will find him?" said Robert p_args again, after a few minutes' silence. "Yes, we'll find him," was Glenarvan's remotely reply, "Thalcave has set us on the track, and I have great p_args confidence in him." "Thalcave is a brave Indian, isn't he?" said the boy. "That indeed he is." $ echo -e 'g/\<remotely\>/.,/\<p_args\>/j\nwq' | ed -sl a $ cat a "Yes, and to be grown up, my child, to venerate him," replied Glenarvan, deeply touched by the boy's genuine affection. During this conversation the horses remotely had been slackeningspeed, and were only walkingnow."Yo +u will find him?" saidRobert p_args again, after a few minutes' silence. "Yes, we'll find him," was Glenarvan's remotely reply, "Thalcavehas set us on the track,and I hav +e great p_args confidence in him." "Thalcave is a brave Indian, isn't he?" said the boy. "That indeed he is." $

If you do mind the words running together, you may need a two-pass solution, eg.

for t in 's/$/ /' j; do echo -e 'g/\<remotely\>/.,/\<p_args\>/'"$t"'\n +wq' | ed -sl filename

(Update: added double quotes in the above.)

(Update: a bit better than the above would be

g='g/\<remotely\>/.,/\<p_args\>/'; echo -e "${g}s/\$/ /\n${g}j\nwq" | +ed -sl filename

)

or some other method, such as

ex -c $'g/\<remotely\>/.,/\<p_args\>/j\nwq' filename

Update: uh, I see only now that your terminator is "p_agrs", whereas I thought it was "p_args". I might replace them later in this node, but for now, you'll have to do the replacement yourself.

Update: did someone mention perl -wpe 'if (/\bremotely\b/.../\bp_args\b/) { chomp }' above? They probably didn't only because they interpreted your question differently.

See Re^2: Joining two files on common field for a list of nodes where unix textutils is suggested to merge files.

Replies are listed 'Best First'.
Re^2: Join lines that match an string
by ranrodrig (Novice) on Jul 13, 2010 at 17:02 UTC

    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

      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

      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/ }'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-24 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found