For the first example to work you need backslashes
before each n (\n).
Discussion on:
View:
Show:
All the examples are missing backslashes everywhere it's as if someone ran s/\\//g over the examples.
That must be a symptom of editing... =( Or the software is
sanitizing things, because that's not how it was submitted.
sanitizing things, because that's not how it was submitted.
Sorry for the confusion. For some reason, the escapes are not being rendered properly in the post. I am working on getting this resolved ASAP.
It all looks great now, thanks. As someone
else has pointed out elsewhere, it would be
best to delete all the posts referring to
the (now non-existent errors) they'll only
be confusing to future readers.
else has pointed out elsewhere, it would be
best to delete all the posts referring to
the (now non-existent errors) they'll only
be confusing to future readers.
This kind of issue--code in a submission is whacked unrecognizable--is endemic to online publishing. If you belong to any Yahoo group I'm sure you've seen URLs with random spaces.
The best solution would be if online publishers gave their authors a "staging" area where the publisher would upload the article and the author could confirm everything works as advertised. This will never happen as all of this information is provided gratis.
So folks, you get what you pay for. You want free code snippets, you get an occasional bug (and misspelling). Rather than rag the author, whose code probably worked in the original manuscript, get out your editor and fix it yourself.
One additional tidbit would be to add how to put these sed commands into a shell script. There are some additional escape sequences you must use to make this work. Any non-trivial sed command takes a while to get it to run well. If it is useful then you will want to keep it in your bag o' tricks. I must have a dozen or so SH files in my bin directory.
doug in Seattle
The best solution would be if online publishers gave their authors a "staging" area where the publisher would upload the article and the author could confirm everything works as advertised. This will never happen as all of this information is provided gratis.
So folks, you get what you pay for. You want free code snippets, you get an occasional bug (and misspelling). Rather than rag the author, whose code probably worked in the original manuscript, get out your editor and fix it yourself.
One additional tidbit would be to add how to put these sed commands into a shell script. There are some additional escape sequences you must use to make this work. Any non-trivial sed command takes a while to get it to run well. If it is useful then you will want to keep it in your bag o' tricks. I must have a dozen or so SH files in my bin directory.
doug in Seattle
"Rather than rag the author, whose code probably worked in the original manuscript, get out your editor and fix it yourself."
This assumes I know what's wrong and how to fix it. I don't think anybody here was ragging the author. Some of us were just confused.
This assumes I know what's wrong and how to fix it. I don't think anybody here was ragging the author. Some of us were just confused.
What's the purpose of the 'n' character in "line onenline twon"? Is it some sort of text separator? How does the sed command know to interpret the 'n' after 'one' and 'two', but not the 'n' in the two occurences of the word 'line'?
Each 'n' should actually be the new line
sequence \n (i.e. n preceded by the escape
character).
sequence \n (i.e. n preceded by the escape
character).
That would explain why the output wasn't
( li e o e li e two )
or
( li )
( e )
( o )
( e )
( li )
( e )
( two )
depending on how I misinterpreted the character
( li e o e li e two )
or
( li )
( e )
( o )
( e )
( li )
( e )
( two )
depending on how I misinterpreted the character
You're absolutely right to mock my reply to
your question. I should have written it
more carefully, but you've obviously more
than understood what I meant to put.
Perhaps I should have just corrected the
line: printf "line one\nline two\n" |
sed -e 's/.*/( & )/'
your question. I should have written it
more carefully, but you've obviously more
than understood what I meant to put.
Perhaps I should have just corrected the
line: printf "line one\nline two\n" |
sed -e 's/.*/( & )/'
I'm grateful for it.
If I'm mocking anything, it's the original article. I'm -very- new to Linux and was not familiar with the \n line separator before your reply. Since I didn't know it existed, I didn't realize the '\' was missing, so the 'n' characters confused the 'l' out of me.
If I'm mocking anything, it's the original article. I'm -very- new to Linux and was not familiar with the \n line separator before your reply. Since I didn't know it existed, I didn't realize the '\' was missing, so the 'n' characters confused the 'l' out of me.
That should be "line one\nline two"... let's see if the
comments print it out properly.
I've just sent an email to my editor to see if those examples
can be fixed.
comments print it out properly.
I've just sent an email to my editor to see if those examples
can be fixed.
chain several sed commands together.
Some stream edits can be quit long and involved.
To ease the complexity and increase the understandability, chain several sed commands together:
For example:
ls -alF | sed -e 's/Jan /01-/' | sed -e 's/Feb /02-/' | ...
to replace Jan with 01-, Feb with 02- and so on.
Trivial example but illustrates the point.
Some stream edits can be quit long and involved.
To ease the complexity and increase the understandability, chain several sed commands together:
For example:
ls -alF | sed -e 's/Jan /01-/' | sed -e 's/Feb /02-/' | ...
to replace Jan with 01-, Feb with 02- and so on.
Trivial example but illustrates the point.
Maybe I'm just not enough of a programmer to follow this article well, or maybe my Kubuntu is broke (but it's not)... but here are the results of my commands:
eljefe@home:~$ printf "line onenline twon" | sed -e 's/.*/( & )/'
( line onenline twon )eljefe@home:~$
eljefe@home:~$ printf "line onenline twon" | sed -e 's/(.*) (.*)/2,1/'
line onenline twoneljefe@home:~$
Basically nothing was broken down like you showed that it would; it just became a part of the next line, before my prompt.
Part of the reason, in my head, is that you didn't give a single example of what was supposed to be the delimiter in each example. How would the command know that some random letter 'n' would mean 'line break' (it wasn't a \n in your example, but maybe it was supposed to be... editors?).
The -e flag, for those who have to look (me), means there is a script involved, or so says the help screen. Where is the script? I feel like I am missing a lot here.
The final example, although the most difficult, seemed to me to be the most accurately explained. This is one that I would like to test a few times, and then I would use it often if it works better than the first two.
Thanks for the article, but please write it so that we can all understand it, with real examples, not variables in place of variables in place of a useful example (yes I feel that you're two levels away from reality here). But maybe its just me, that's been known to happen also.
eljefe@home:~$ printf "line onenline twon" | sed -e 's/.*/( & )/'
( line onenline twon )eljefe@home:~$
eljefe@home:~$ printf "line onenline twon" | sed -e 's/(.*) (.*)/2,1/'
line onenline twoneljefe@home:~$
Basically nothing was broken down like you showed that it would; it just became a part of the next line, before my prompt.
Part of the reason, in my head, is that you didn't give a single example of what was supposed to be the delimiter in each example. How would the command know that some random letter 'n' would mean 'line break' (it wasn't a \n in your example, but maybe it was supposed to be... editors?).
The -e flag, for those who have to look (me), means there is a script involved, or so says the help screen. Where is the script? I feel like I am missing a lot here.
The final example, although the most difficult, seemed to me to be the most accurately explained. This is one that I would like to test a few times, and then I would use it often if it works better than the first two.
Thanks for the article, but please write it so that we can all understand it, with real examples, not variables in place of variables in place of a useful example (yes I feel that you're two levels away from reality here). But maybe its just me, that's been known to happen also.
This is a potentially useful article, but either the formatting or the editing rendered it less effective than it could have been. If it gets cleaned up, it may be worth deleting the posts about the formatting problem since they'll just confuse future readers.
The formatting of the platform is what wrecked things. It
should be ok now. I'm not able to delete posts, but the
examples are properly showing the "\n" bit now, so hopefully
it will be easier to read.
should be ok now. I'm not able to delete posts, but the
examples are properly showing the "\n" bit now, so hopefully
it will be easier to read.
and basically if I was going to learn sed, grep or awk that I shouldnt.
anything that can be done with any of these tools can be done with perl on the command line (without writing a script).
It gives you more power, only means you need to learn the syntax of one tool and is typically a much more useful tool to learn. Thats not to say you need to learn all of perl, just the sed/awk/grep or whatever part of it.
they are all fairly similar and hence its not harder to learn.
I gave the same advice to someone who was already very proficient with sed and awk and now uses perl exclusively for the same things.
my 2c
anything that can be done with any of these tools can be done with perl on the command line (without writing a script).
It gives you more power, only means you need to learn the syntax of one tool and is typically a much more useful tool to learn. Thats not to say you need to learn all of perl, just the sed/awk/grep or whatever part of it.
they are all fairly similar and hence its not harder to learn.
I gave the same advice to someone who was already very proficient with sed and awk and now uses perl exclusively for the same things.
my 2c
- Keyboard Shortcuts:
- Prev
- Next
- Toggle

































