Search code examples
bashseddelimiter

sed separator (delimiter) replacement not working with p command


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 a line that begin by 4.3 (chapter 4.3 selecting lines by text matching) to the next line that begin by 5 (chapter 5 Regular Expressions: selecting text) ,
      • On lines that begin by a quote ' (or a UTF8 open quote: ),
        • place a label a for further branch (goto),
        • append then next line,
        • if current buffer does not contain a newline followed by one space, the branch to label a.
        • append one more line and
        • print out the current buffer.

    This will extract, from info sed, the following:

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

    In fine, regarding your question:

    So you have to precede your 1st delimiter by a backslash \:

    $ echo A | sed -ne '\#A#p'
    A