![]() |
sed script conundrum...sigh
Source text-
Code:
Title: King KongTitle: GodzillaCode:
'\BTitle: 'Code:
\<Code:
sed 's/\BTitle:/Title:^M/' title.txtThe wildly different implementations of regex are really p****g me off! |
If the "Title" you are looking for is at the start of the line, you can just use the '^' anchor to match it:
's/^Title:/Title:^M/' |
I should have included my desired result which is
Quote:
|
Quote:
|
Oh - finally I understand.
I thought you were trying to avoid matching "Title:" except at the beginning of a line. Instead you are trying to break up lines that have "Title:" in the middle of them. I'm not sure how to do this in 'sed'. I too find 'sed' a bit frustrating. I would advise doing everything in Perl. It's more powerful and has fewer exceptions. E.g.: perl -p -e 's/^(.+)Title:/\1\nTitle:/g' title.txt |
With-
Code:
sed 's/^Title:/^MTitle:/' title.txtTitle: King KongTitle: Godzilla Title: Mothra But it should be: Title: King Kong Title: Godzilla Title: Mothra BTW- the ^M is a substitute for \r because the shell is choking on \r. |
I was preparing to ditch sed for perl when I was stopped abruptly by many posts telling me, "well if you're going to spend the time to learn Perl, you'd be better off learning Python instead." Or variations like that. And before I knew it, I was considering learning a major programming language to do some text cleanup. Ay carumba!
|
Quote:
|
Quote:
|
Quote:
I've heard a lot of people like it and that it is growing in popularity. I think there still is more Perl software (modules) available than there is for Python, but that may well change. One big difference between Perl & Python is that white-space (e.g. indentation) is significant in Python. I don't like that, but maybe it's all about what you are used to. |
A little awkward perhaps:
Code:
sed 's/Title/\newline (literally). The pipe just deletes blank lines. Not a bona fide "one-liner" granted... but it's pure sed (sick editor). ;) -HI- |
Hal i tosis-
That was brilliant. thx. Try a tongue scraper. |
Quote:
Fotmasta: You are trying to do two things at once here: change case and add a newline. While in this simple case it is certainly possible with something like /[Tt]itle/\ntitle/ you can also use multiple -e sections and pipes, like Hal did: sed -e 's/Title/title/g' -e 's/title/\ntitle/g' | sed -e '/^$/d' Unlike hayne, I prefer sed over perl. I don't need the extra functions of perl and I don't have time to wade through piles of documemtation to figure it out. It boils down to what you prefer, and learning time vs. coding time. |
I think I goofed on the original post. I inadvertantly changed text.
Let's start with this mashed up text, which needs to be split apart. Code:
STARTthx, FM |
If you don't insist on sed as the means, this AppleScript will do the deed:
set input to "title: King Kongtitle: Godzillatitle: Mothra" set T to "title: " set R to return set {TID, text item delimiters} to {text item delimiters, T} set str to text items of input set text item delimiters to TID tell str to set out to T & item 2 & R & T & item 3 & R & T & item 4 |
Quote:
the 'T' in my solution into a 't'... :rolleyes: or even [Tt] for full coverage. Quote:
|
Quote:
Quote:
Applescript: full-featured read-only programming language. |
Quote:
|
Quote:
Oh, geez I just sent my own post off onto a tangent. (I have to stall while I wait for our illustrious IT professionals to update gnu sed to 4.1.4. I am ready to hear- "install what now?") ... |
Perl is often referred to as a write-only language because it can be nearly impossible to figure it out later unless the script is heavily documented. There is an annual "Perl Obfusication Contest" where people try hard to write the most cryptic, unintellegibe perl program that still does something useful.
Applescript is, in comparison, very verbose and easy to read. But writing it (especially for people used to terser languages) can be frustrating. "Set variable a to the text value of the property of the box with name "input" of the foreground window" vs. "$a = $_FORM["input"]" Nothing to do with wether you CAN write it, but how easy it is to do. If you have administrator access to your machine I can send you a binary. Try this in the terminal as a quick test: mkdir /sw If it succeeds, we're in business. |
Quote:
And note that the original Obfuscated Code contest was for C language programs: http://en.wikipedia.org/wiki/Obfuscated_code |
Quote:
|
Apple ][ BASIC wasn't too bad. But with <30kb usable memory it couldn't get too complicated. I think GOTO and GOSUB were the only loop options other than FOR-NEXT. I hope my school's CompSci teacher didn't need too much therapy after our class. (We drove the Gr.10 algebra teacher to a nervous breakdown).
6502 assembly code did goto, comments were maybe 20 characters each, but then no one expects machine code to be easy to read later. I'll stop now before I date myself too much :D |
The first machine I ever wrote code for was a Royal McBee LGP30 - a hard dating to beat, I suspect. Its memory was a rotating drum with 80 read heads on it, 4096 words in total.
http://www.users.nwark.com/~rcmahq/jclark/lgp30.jpg |
| All times are GMT -5. The time now is 05:29 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.