Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A tricky sed problem 2

Status
Not open for further replies.

butterfm

Programmer
Feb 15, 2002
132
GB
Hi,

I have similar to the following text :

I would like this[Unloading randomclass1]
[Unloading randomclass2]
[Unloading randomclass3]
[Unloading randomclass4]
to be a single sentence


I need to end up with :

I would like this to be a single sentence

There number of lines between the required parts is unknown as is the text (it differs from line to line)

What I have so far is this :

sed '/Unloading/{
N
s/\[Unloading.*\n//g
}' somefile


But all this gives me is :

I would like this[Unloading randomclass2]
[Unloading randomclass4]
to be a single sentence


because it doesn't re-process the line again once it has brought the next line up.

Any sed Guru's who may be able to help ?

Thanks,
Matt.
 
This is not that simple really because the filter needs to know when to insert a space and when not to.
I have a perl pipeline that does it.
Code:
echo 'I would like this[Unloading randomclass1]
[Unloading randomclass2]
[Unloading randomclass3]
[Unloading randomclass4]
to be a single sentence
' | perl -ne 's/\[Unloading randomclass.\]\n/ /g;print;' | perl -lne 's/\s\s+/ /g;print'
produces: "I would like this to be a single sentence"

Is that any use?


Trojan.
 
Hi

At first glance :
Code:
sed -n 's/\[Unloading.*\]//; H; ${ g; s/\n//g; p }' somefile

This will output the whole text in one line. You did not specified if there is other text to keep in separate line beside the mentioned sentece.

Feherke.
 
Pinching feherke's idea I got:
Code:
echo 'I would like this[Unloading randomclass1]
[Unloading randomclass2]
[Unloading randomclass3]
[Unloading randomclass4]
to be a single sentence
' | sed -n 's/\[Unloading.*\]/ /; H; ${ g; s/\n//g; p }' | sed -n 's/   */ /g; p'


Trojan.
 
Thanks guys. Apologies but I didn't give quite enough detail.

The file may be something like this any number of times.

I would like this[Unloading randomclass1]
[Unloading randomclass2]
[Unloading randomclass3]
[Unloading randomclass4]
to be a single sentence
Ignore this line here
I would like this[Unloading randomclass1]
[Unloading randomclass2]
[Unloading randomclass3]
[Unloading randomclass4]
to be a single sentence


The results I would like to get out are :


I would like this to be a single sentence
Ignore this line here
I would like this to be a single sentence


Apologies for the lack of detail.

Thanks,
Matt.

 
TrojanWarBlade,

Your solution is very nearly there. The only issue remaining is that I get everything on one line.

e.g.

I would like this to be a single sentenceIgnore this line hereI would like this to be a single sentence

Thanks,
Matt
 
Hi

With minimal changes, but supposed that you allways have more then one consecutive Unloading things :
Code:
sed -n 's/\[Unloading.*\]//; H; ${ g; s/\n\{2,\}/ /g; p }' sentence.txt

Feherke.
 
Thanks very much to both of you. Feherke's solution works a treat. As far as I can see there'll always be more than one consecutive "Unloading" line.

I think I need to do a lot more reading up on sed.

Many thanks,
Matt.
 
feherke is obviously much more of a "sed" guy than I.
Well done feherke.


Trojan.
 
I used to write awk and sed many years ago but I haven't bothered for nearly 10 years since I got into perl.
I find that sed and awk can be a little more succinct on occasion but generally, perl is far more consistent, powerful, versatile and efficient. Hence the lack of use of any other scripting languages.


Trojan.
 
Hi feherke - i just looked at that aurelio link you posted... and now i am crying too

That is just not normal - he hasn't put a picture of himself on the web page... because he must be green with big ears!


Kind Regards
Duncan
 
Come on Duncdude,
Don't pretend that you couldn't have writtent that Sokoban in your sleep! (or in your dreams maybe? ;-) )


Trojan.
 
more akin to nightmares than dreams! :-(


Kind Regards
Duncan
 
Here's a Gawk or Mawk version:
Code:
BEGIN {RS="(\\[[^\n]*\\]\n)+"; ORS=" "}
8
This part is executed before any lines are read:
Code:
BEGIN {RS="(\\[[^\n]*\\]\n)+"; ORS=" "}
allowing the main loop to be quite small:
Code:
8

ferherke said:
[tt]perl -pe 's/\[.*\]\n//; s/\b$/ /' somefile[/tt]
Wouldn't this add a space to
[tt]Ignore this line here[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top