Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

gnu - What delimiters can you use in sed?

We normally see people complaining about the unknown option to s' error in sed when they want to use a pattern that contains the sed delimiter.

For example, if we are using /:

$ var="hel/lo"
$ sed "s/a/$var/g" <<< "haha"
sed: -e expression #1, char 9: unknown option to `s'

So we advise to use another delimiter, for example |:

$ sed "s|a|$var|g" <<< "haha"
hhel/lohhel/lo

However, I want to know what are the possible delimiters sed can accept... since it seems to be almost any character including regex-like ones (*, ?, ., ...)!

In my sed (GNU sed) 4.2.2:

$ sed 's/a/b/g' <<< "haha"
hbhb
$ sed 's_a_b_g' <<< "haha"
hbhb
$ sed 's#a#b#g' <<< "haha"
hbhb
$ sed 's$a$b$g' <<< "haha"
hbhb
$ sed 's?a?b?g' <<< "haha"
hbhb
$ sed 's*a*b*g' <<< "haha"
hbhb
$ sed 's-a-b-g' <<< "haha"
hbhb
$ sed 's.a.b.g' <<< "haha"
hbhb
$ sed 'sXaXbXg' <<< "haha"
hbhb
$ sed 'sxaxbxg' <<< "haha"
hbhb
$ sed 's1a1b1g' <<< "haha"
hbhb

Even a works here if it is escaped:

$ sed 'saaabag' <<< "haha"
sed: -e expression #1, char 5: unknown option to `s'
$ sed 'saaabag' <<< "haha"
hbhb

Is there any specification for this?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Perhaps the closest to a standard, the POSIX/IEEE Open Group Base Specification says:

[2addr] s/BRE/replacement/flags

Substitute the replacement string for instances of the BRE in the pattern space. Any character other than backslash or newline can be used instead of a slash to delimit the BRE and the replacement. Within the BRE and the replacement, the BRE delimiter itself can be used as a literal character if it is preceded by a backslash."


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...