Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Weird! Perhaps the parser is trying to interpret the "[*]" as part of a nonexistent list construct. If you double-space that input, it displays as single-spaced lines.
BTW, watch out for the 'N' option in sed. If it hits an end of file it'll terminate the script prematurely. Here I've added an extra line of input 'seven'.
Code:
test@ws1:/tmp$ sed -e 'N;s/\n*/(*)/' /tmp/sed.in
(*)one
two
(*)three
four
(*)five
six
seven
As you can see, the script terminates prematurely before doing the s/// on the final line when it tries to append(N) the non-existent line 8.
It now gets to the end of the script and does an implicit print of the buffer. It prints '(*)one\ntwo' to the terminal, which displays as
Code:
(*)one
two
I suspect part of your confusion is stemming from the fact you are confusing the meaning of '*' in a regex, with the meaning of '*' in a shell glob. They are quite different.
I hope that clears it up for you.
Really nice explanation. This makes it perfectly clear. I wasn't confused by * actually, I simply didn't understand how sed interpreted it and the 'trick' (which now seems perfectly logical) with the \n line. Now it's clear that the \n remains there where it should be (as it's not matched). I kept thinking there was a \n at the beginning of the first, third, etc. lines (because that's where it matched the string, but concomitantly I knew that \n* could match no \n at all, as rknichols had already explained) - which didn't make sense, yes I think I'm really starting to finally understand N. Thank you!
BTW, watch out for the 'N' option in sed. If it hits an end of file it'll terminate the script prematurely. Here I've added an extra line of input 'seven'.
As you can see, the script terminates prematurely before doing the s/// on the final line when it tries to append(N) the non-existent line 8.
So basically the newline isn't matched (as there's no additional line) and that's why there's nothing to substitute and it ignored the last line, if my understanding is correct.
P.S. By the way, I used square brackets within codes by pressing an addition 'enter' where I knew the line would split and it works like that, even if it's not so practical with a long quoted text.
on the last line N could not be executed. As you stated before already \n* matches anyway (including the empty string), so the real reason is: sed stopped at the command N (because it failed) and did not try to substitute any more.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.