Issue
I try to replace /
with #
separators in sed print command but don't succeed. Please look at the third line beneath:
u@debian:~$ echo A | sed -n '/A/p'
A
u@debian:~$ echo A | sed -n '#A#p'
u@debian:~$ echo A | sed -n s#A#B#p
B
u@debian:~$
How to replace it?
Solution
Preamble
Reading the title of your question, I think you have to read quietly the address chapter*
in info sed
!
Understanding the difference between addressing and commands! s
, like p
are commands!
So your request is about addressing for executing a command.
Address range and address for commands
Little sample: comment all lines that contain badWord:
sed -e '/badWord/s/^/# /' -i file
More complete sample:
info sed |
sed -ne '
/^4.3/,/^5/{
/^\(\o47\|\o342\o200\o230\){
:a;
N;
/\n / ! ba;
N;
p
}
}'
- From the line that begin by
4.3
to the line that begin by5
,- On lines that begin by a quote
'
(or a UTF8 open quote:‘
),- Place a label
a
. - Append next line
- If current buffer does not contain a newline followed by one space, the branch to label
a
. - Append one more line
- print current buffer.
- Place a label
- On lines that begin by a quote
'/REGEXP/'
This will select any line which matches the regular expression
REGEXP. If REGEXP itself includes any '/' characters, each must be
'\%REGEXP%'
(The '%' may be replaced by any other single character.)
'/REGEXP/I'
'\%REGEXP%I'
The 'I' modifier to regular-expression matching is a GNU extension
which causes the REGEXP to be matched in a case-insensitive manner.
'/REGEXP/M'
'\%REGEXP%M'
The 'M' modifier to regular-expression matching is a GNU 'sed'
extension which directs GNU 'sed' to match the regular expression
'/[0-9]/p' matches lines with digits and prints them. Because the
second line is changed before the '/[0-9]/' regex, it will not match and
will not be printed:
$ seq 3 | sed -n 's/2/X/ ; /[0-9]/p'
1
'0,/REGEXP/'
A line number of '0' can be used in an address specification like
'0,/REGEXP/' so that 'sed' will try to match REGEXP in the first
'ADDR1,+N'
Matches ADDR1 and the N lines following ADDR1.
'ADDR1,~N'
Matches ADDR1 and the lines following ADDR1 until the next line
whose input line number is a multiple of N. The following command
Please, RTFM:
Have a look at info sed
, search for * sed addresses
, then * Regexp Addresses
:
‘/REGEXP/’ This will select any line which matches the regular expression REGEXP. If REGEXP itself includes any ‘/’ characters, each must be escaped by a backslash (‘\’).
...
‘\%REGEXP%’ (The ‘%’ may be replaced by any other single character.) This also matches the regular expression REGEXP, but allows one to use a different delimiter than ‘/’. This is particularly useful if the REGEXP itself contains a lot of slashes, since it avoids the tedious escaping of every ‘/’. If REGEXP itself includes any delimiter characters, each must be escaped by a backslash (‘\’).
In fine, regarding your question:
So you have to precede your 1st delimiter by a backslash \
:
$ echo A | sed -ne '\#A#p'
A
Answered By - F. Hauri - Give Up GitHub Answer Checked By - Robin (WPSolving Admin)