Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I don't understand what the following script does:
sed -e '
:more
/^$/N
s/^\n//
tmore
'
Can anyone give me the explaination?
Joe

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 branchI 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

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.

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'

![]() |
Please Help with skript (...
|
my script doesnt work in ...
|

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