Sunday, 12 September 2010

miriam_e: from my drawing MoonGirl (Default)
Amazing! Now that the government has finally been installed they are concerned with trying to get together "the best team" to work on the same old crap. Don't any of them get it?

The reason it took so long to work out what party lost the election is that people generally didn't want either of the big parties. Both represent the big corporations (particularly big energy and big media) instead of the public. They are absurdly out of step with the Australian people.

I was kinda hoping that the big parties' leaders would make their first action an apology to us all. Fat chance! Seems they are as deluded as ever.

Oh well, in a way it is a good thing. This way it is made thoroughly clear how useless they are. If they'd been smarter and talked about actually listening to the population instead of the powerful companies then people might have been taken in.

The biggest worry in all this is that I think the major use of government is in being a force to slow the predations of giant corporations and to help redistribute wealth to the wider community. If government is failing then we need some other way to slow them. But what?
miriam_e: from my drawing MoonGirl (Default)
I'd always intended to read some Raymond Chandler stories, so the past several days I've been reading some of "The Big Sleep" each night before bed, or during the night if I wake and my mind is too active to let me return to slumber.

In the beginning I was almost dreading it because some months back I read Dasheil Hammett's "The Maltese Falcon" and was surprised at how little I liked it. Also, I'd read some background on Raymond Chandler and how insulting the literature critics had been about his work. I should know by now that if critics like something then it is usually pretentious crap, but if they dislike it then it is often good work -- not a fool-proof guide of course, but correct often enough that I should utterly disregard critics' opinions.

I finished it this morning and have to say that I quite enjoyed "The Big Sleep". Even with its abrasive homophobia it is still an absorbing piece of work. One of the things I particularly enjoyed was how he likes to play with words and phrases in novel ways. Strangely, his main character is likeable even though he is not very pleasant or charming. The story plays its puzzle out, without being obtuse, right up to nearly the end. A very well done piece of work. I wish I could write a fraction as well. It just shows, once again, how stupid critics can be too.
miriam_e: from my drawing MoonGirl (Default)
 
Another Edit: One tiny extra change I added was to make the joining of lines conditional so that it was only done if the current line is non-blank.

Edit: Holy cow! I woke up at 3:30am, and was unable to get back to sleep because I had come up with a simpler way to do this. I've now stuck most of the original post behind a cut tag and present the much better way to do paragraph wrapping in sed below.

Woo hoo!!!

I have finally managed to work out a small command that has stymied me for an embarrassingly long time.

A quick backgrounder: Years ago I bought a copy of a truly marvelous text editor called TextPad. Since I've moved to Linux I have had to use TextPad inside Wine, which is really annoying. I'd like to drop TextPad and use a native Linux text editor, but I've yet to find one that comes even close to its capabilities. One of the major failings of all Linux text editors that I've found is the lack of an intelligent text wrap. That is, something that removes line-endings from lines within paragraphs but keeps line endings on the blank lines that separate paragraphs.

Okay, the new, improved way:

sed ':a; /^$/!N; /\n$/!s/\n/\ /; ta' input.txt >output.txt

Just look at how small that is. No need to double blank lines or insert an arbitrary string into the text to mark the ends of non-blank lines. This little gem sees the newline character itself. And I worked out how to eliminate the two "-e" options too.

How it works:

We start with a label (:a) that marks the beginning of a loop.

The semicolon (;) simply separates sed commands.

The first thing we do is to begin with a condition (/^$/!) which means "if the current line is not blank" ('^' stands for the beginning of a line, '$' stands for the end of a line, and '!' means 'not'). So, if the current line is not blank then use the "N" command to append the next line to this one.

Then we set a condition (/\n$/!) that lets the rest of the loop work only if a newline (\n) is NOT (!) at the end of line ($).
This is tricky and is the insight that wouldn't let me get back to sleep. Easiest to see the logic if you follow its two possibilities: If the line being appended has text on it then the newline character at the end of the current line will be wedged between the text on this one and the next one. That is, it won't be at the end. However, if the next line being appended is an empty line then the only thing that will be appended will be a newline... at the end of the current line. Neat, huh?

If the newline was not at the end of the line (the next line was not a blank line) then we simply substitute (s) the newline (\n) with a space (\ ).

The last bit (ta) closes the loop.

So this loop ripples through a paragraph replacing newline characters with spaces til it finds a blank line, which it leaves alone, drops out of the loop, then does the whole command on the next line, which probably being another paragraph gets the same treatment. This whips through the whole file in a fraction of a second.

So my final comments are even more true of this revised version:

Yay! Hard to believe that this was so damn hard to work out. It appears pretty easy, looking at it now. Still... 20/20 hindsight and all that.


if you're curious to see the old way click here )
Page generated Wednesday, 20 May 2026 09:23 am
Powered by Dreamwidth Studios