Discussion:
errors in sed manual 4.2.2
(too old to reply)
bamber ward
2014-08-09 13:13:46 UTC
Permalink
Unfortunately, errors in the documentation seem to persist. Somewhat
disapointing but correcting
bugs is, I find, a good way to really understand a language.

Section 4.7
add
s/.*// after last h
to see why just run the program as given in the manual.

Sections 4.9 and 4.10
in S!{
h
b
}
add s/.*// after h
omit final p
without these changes you get, for
a file containing,
the cat sat
on the mat # no new line here
baa
23
23

instead of 23

4.15
I see you have added the -n option to this edition to try to correct this
script.
However, the comment in the script should state print the second(not first)
duplicated line
The program still does not give the correct output
cat uniq_d3.txt
dog
dog
cat
cat
pony
gives output:
dog
cat
cat
pony

instead of :
dog
cat

The script prints out the last line regardless and if the last two lines
are different it prints both.
Replacing the last $b by
$ s/.*/ / seems to fix it
Possibilities
Last two lines different.Can't be duplicates so delete
Last two lines duplicates.
Last line added in the code block.
Last but one line deleted
Then Last line printed so ok to delete pattern space at end.
4.17
Need to remove the first p in the last script to avoid duplicated output

I hope you find this helpful
Best Wishes

David
bamber ward
2014-08-09 14:15:51 UTC
Permalink
I have tried to construct examples to test out \`(backslash backtick) but
have not succeeded in getting the responses I want. I assume that
backtick is the same as backquote. Backticks as in `command`.
for instance,
echo cat | sed "s/\`cat/bingo/"
gives cat
echo cat | sed "s/\`cat\'/bingo" # double quotes because of single quote
gives cat
but
echo cat | sed "s/cat\'/=/" gives
=
Post by bamber ward
Unfortunately, errors in the documentation seem to persist. Somewhat
disapointing but correcting
bugs is, I find, a good way to really understand a language.
Section 4.7
add
s/.*// after last h
to see why just run the program as given in the manual.
Sections 4.9 and 4.10
in S!{
h
b
}
add s/.*// after h
omit final p
without these changes you get, for
a file containing,
the cat sat
on the mat # no new line here
baa
23
23
instead of 23
4.15
I see you have added the -n option to this edition to try to correct
this script.
However, the comment in the script should state print the second(not
first) duplicated line
The program still does not give the correct output
cat uniq_d3.txt
dog
dog
cat
cat
pony
dog
cat
cat
pony
dog
cat
The script prints out the last line regardless and if the last two lines
are different it prints both.
Replacing the last $b by
$ s/.*/ / seems to fix it
Possibilities
Last two lines different.Can't be duplicates so delete
Last two lines duplicates.
Last line added in the code block.
Last but one line deleted
Then Last line printed so ok to delete pattern space at end.
4.17
Need to remove the first p in the last script to avoid duplicated output
I hope you find this helpful
Best Wishes
David
Davide Brini
2014-08-09 14:11:38 UTC
Permalink
Post by bamber ward
Unfortunately, errors in the documentation seem to persist. Somewhat
disapointing but correcting
bugs is, I find, a good way to really understand a language.
Section 4.7
add
s/.*// after last h
to see why just run the program as given in the manual.
Perhaps you haven't noticed that the program needs to be run with sed -n,
as shown in the shebang line. And in any case, why "s/.*//" and not just
"d"?

With sed -n, the program works fine.
Post by bamber ward
Sections 4.9 and 4.10
in S!{
h
b
}
add s/.*// after h
omit final p
Same thing, you need to run with -n.
Post by bamber ward
without these changes you get, for
a file containing,
the cat sat
on the mat # no new line here
baa
23
23
instead of 23
That's because you aren't running with -n.

Btw, a file without the newline character on the last line is not a
well-formed text file.
Post by bamber ward
4.15
I see you have added the -n option to this edition to try to correct
this script.
However, the comment in the script should state print the second(not
first) duplicated line
This is correct.
Post by bamber ward
The program still does not give the correct output
cat uniq_d3.txt
dog
dog
cat
cat
pony
dog
cat
cat
pony
dog
cat
Run with sed -n and you'll get the the expected result.
--
D.
Davide Brini
2014-08-09 14:46:01 UTC
Permalink
Post by bamber ward
I have tried to construct examples to test out \`(backslash backtick) but
have not succeeded in getting the responses I want. I assume that
backtick is the same as backquote. Backticks as in `command`.
for instance,
echo cat | sed "s/\`cat/bingo/"
gives cat
You're hitting shell escaping issues. If you're in doubt, a way to check is
putting the sed program in a file, then running with sed -f prog.sed.

In this first example, sed sees:

s/`cat/bingo/

as your shell is most likely removing the backslash before the backtick.
So the result is correct: no replacement is made, as the input does not
contain

`cat

Instead, you probably want one of these:

$ echo cat | sed 's/\`cat/bingo/'
bingo
$ echo cat | sed "s/\\\`cat/bingo/"
Post by bamber ward
echo cat | sed "s/\`cat\'/bingo" # double quotes because of single quote
gives cat
As written, that gives an error:

sed: -e expression #1, char 14: unterminated `s' command

If, as I think, you meant instead

echo cat | sed "s/\`cat\'/bingo/"

then again the shell removes the first backslash and sed sees

s/`cat\'/bingo/

and the result is again correct, no replacement as the input does not
contain

`cat

Working alternatives:

$ echo cat | sed "s/\\\`cat\'/bingo/"
bingo
$ echo cat | sed 's/\`cat'\\\''/bingo/'
bingo
Post by bamber ward
but
echo cat | sed "s/cat\'/=/" gives
=
Here sed sees

s/cat\'/=/

so there is a match and the replacement is performed.

Note that \` and \' are GNU sed specific extensions and will not work on
other sed implementations.


On a related note, it's quite difficult to find the download link for the
latest source, as this page is outdated:

http://directory.fsf.org/wiki/Sed

and here

http://www.gnu.org/software/sed/sed.html

there's no download link, nor do the docs
(http://www.gnu.org/software/sed/manual/) say where it can be downloaded.
And finally, http://www.gnu.org/software/sed/manual/sed.html seems still
to be for 4.2.1.
--
D.
Loading...