You are not logged in.
Hi!
I'm trying to delete lines/files from a list of file names (with paths / slashes) in a txt with sed. Did't work, took me a while to find the problem:
$ echo "test\nfoo\ndoh" > testfile.tmp
$ sed "#foo#d" "./testfile.tmp"
test
foo
doh
$ sed "/foo/d" "./testfile.tmp"
test
doh
The question is... WHY!?
I know a few ways to work around this, but none that aren't slower (part of the expression comes from a variable and contains lots of slashes).
And I don't understand why "#" isn't working as I expected. Do I have to choose a different delimiter for "/d"? Is there one besides slash that works?
Last edited by whoops (2016-02-19 10:41:23)
Offline
/regexp/
Match lines matching the regular expression regexp.\cregexpc
Match lines matching the regular expression regexp. The c may be any character.
So:
$ printf "test\nfoo\nbar\n" > testfile
$ sed '\#foo#' testfile
test
bar
Last edited by ayekat (2016-02-19 09:44:45)
Offline
oh... right.. thanks!
sed "\#foo#d" ./testfile.tmp
test
doh
I should have payed more attention to the red colour of the "c" and less attention to "how I've been doing it with the 's/' version"
Now if someone can explain this to me, then I finally understand the whole man page for sed :
cat testfile.tmp | sed "s\#foo#wtf#"
sed: -e expression #1, char 11: unterminated `s' command
cat testfile.tmp | sed "s#foo#wtf#"
test
wtf
doh
Undocumented "feature" or am I missing something again?
Also: Shouldn't "#foo#d" give an error instead of just (apparently) not doing anything?
Last edited by whoops (2016-02-19 10:02:43)
Offline
The backslash for a simple matching is there to explicitly mark the following character as a delimiter, because it could conflict with an existing command like s or y, or # (a comment).
For substitution, this is not necessary (or as we see in your example, wrong), since it is clear that the subsequent character is a delimiter.
But that's just guessing
Offline
Argh... now I'm thourougly confused:
$ sed "\/^foo$/d" ./testfile.tmp
test
doh
$ sed "\#^foo$#d" ./testfile.tmp
sed: -e expression #1, char 7: unterminated address regex
How do I get the "endofline" working with "#"? I'm starting to hate that man page T_T xD
edit: have to escape it for some reason. Don't understand why.
echo "test\nfoo\nfoobar\ndoh" >| testfile.tmp
sed "\#^foo\$#d" ./testfile.tmp
test
foobar
doh
I'm beginning to suspect that sed just likes doing random stuff because it's evil. First it pretends to be the most useful thing ever for a few years and then when you least suspect it, "*BAM* you're insane".
Last edited by whoops (2016-02-19 10:40:11)
Offline
Use single quotes (or escape the dollar sign).
Here, your shell interprets $# (which denotes the number of arguments to the interpreter), and thus for sed there is no second # that would terminate the regex (hence the error message).
Offline
OOooh, now I think I understand. Thanks. The world is ok again
Offline