Computing.Net > Forums > Unix > script in sed

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

script in sed

Reply to Message Icon

Name: tspj88
Date: February 18, 2004 at 11:26:34 Pacific
OS: Unix
CPU/Ram: 512
Comment:

I don't understand what the following script does:
sed -e '
:more
/^$/N
s/^\n//
tmore
'
Can anyone give me the explaination?
Joe



Sponsored Link
Ads by Google

Response Number 1
Name: Dlonra
Date: February 18, 2004 at 17:23:53 Pacific
Reply:

i am not a sed mayven, so i read the man page (man sed) which includes
: label - Label for b and t commands
n N Read/append the next line of input into the pattern space
t label - conditional branch

I also made a test file to edit
So, /^$/N gets rid of blank lines
not sure of the rest

-e is not needed

let me know when you unravel the rest


0

Response Number 2
Name: tspj88
Date: February 18, 2004 at 18:27:02 Pacific
Reply:

In my test, I had following findings:
/^$/N
search empty lines and N tells to search pattern that are split into 2 lines or immediately quit when reading the End-of-File.
s/^\n//
remove the newline characters
So, I got the same conclusion:
this can remove the empty lines from the input.


0

Response Number 3
Name: Jim Boothe
Date: February 20, 2004 at 14:50:59 Pacific
Reply:

Yes, that code eliminates empty (null) lines. For each null line encountered, it pulls the next line into the pattern space (main work buffer), then deletes the null line at the front of the pattern space. Doing it that way requires for it to loop so that it can delete multiple null lines in succession. Without the loop, if it encountered several null lines in succession, it would delete only the first of each pair of null lines.

As tspj88 says, the s/^\n// is what actually eliminates the null line. That substitution command eliminates the newline character, which is all that a null line is composed of. But that newline character would not be available for elimination with a substitution command without pulling a second line into the buffer with the N command. You cannot eliminate the newline character at the end of the only (or last) line in the pattern space. If you have 5 lines in the pattern space, you can act upon the 4 newline characters that separate those lines.

A much simpler way to do the same thing would be:

sed '/^$/d'


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Please Help with skript (... my script doesnt work in ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: script in sed

parameters in sed www.computing.net/answers/unix/parameters-in-sed/6942.html

Unix script in DOS w/ questions www.computing.net/answers/unix/unix-script-in-dos-w-questions/5848.html

Equivalent perl script for sed www.computing.net/answers/unix/equivalent-perl-script-for-sed/5939.html