You are not logged in.

#1 2016-02-19 09:34:55

whoops
Member
Registered: 2009-03-19
Posts: 891

[solved] sed delete lines with slashes in string, "#" not working

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

#2 2016-02-19 09:44:20

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: [solved] sed delete lines with slashes in string, "#" not working

sed (1) wrote:

       /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)


pkgshackscfgblag

Offline

#3 2016-02-19 10:00:33

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved] sed delete lines with slashes in string, "#" not working

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 big_smile :

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

#4 2016-02-19 10:11:25

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: [solved] sed delete lines with slashes in string, "#" not working

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 smile


pkgshackscfgblag

Offline

#5 2016-02-19 10:31:30

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved] sed delete lines with slashes in string, "#" not working

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

#6 2016-02-19 10:39:09

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: [solved] sed delete lines with slashes in string, "#" not working

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).


pkgshackscfgblag

Offline

#7 2016-02-19 10:41:10

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved] sed delete lines with slashes in string, "#" not working

OOooh, now I think I understand. Thanks. The world is ok again big_smile

Offline

Board footer

Powered by FluxBB