Need assistance with sed

Hi All,

I need your assistance, I would like to replace all lines beginning with the word "begin" with the below text:

       Device          |            IPMB0-A              |             IPMB0-B             
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr     

How this is achievable using sed command line or sed script?
In general, How can I replace a complete line with a certain text?

The below command will only replace the word "begin" but how to replace the complete line:

#sed s/^begin/text/ test.log

Thanks in advance.

Something like:

sed s/^begin.*/text/

Thanks for your quick reply but how about the first example i have highlighted? this is not achievable via command line, it needs sed script to do it. any ideas?

# cat infile
begin this is
this is ...................
that is.................
begin this is
# cat text
       Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
# sed "s/^begin.*/$(sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' text)/" infile
      Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
this is ...................
that is.................
      Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr

Thanks a lot! it worked perfectly :slight_smile: can you explain the command in details please?

BR

sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' text

this command's result is

       Device          |            IPMB0-A              |             IPMB0-B\nBoard      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr

so we add to newline chr as text '\n' char because sed is expect '\n' char for write (in buffer's) 2.line to as our stdoutput newline. that's all :slight_smile:

regards
ygemici

Sorry but this command is giving me an error:

#sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' header 
Unrecognized command: $! s/$/\\n/;

Hi Dendany83,

Another option if the text you want to replace always contains 2 lines:

cat inputfile
begin this is
this is ...................
that is.................
begin this is
-------------------------------------------------------------------------------------------------
text1="Device          |            IPMB0-A              |             IPMB0-B"
text2="Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr"
 
sed "s/^begin.*/$text1\n$text2/" inputfile
Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
this is ...................
that is.................
Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr

Hope this helps,

Regards

It is giving an error:

Label too long: text1="        Device          |             IPMB0-A              |             IPMB0-B              "
#!/bin/sed -f

text1="        Device          |             IPMB0-A              |             IPMB0-B              "
text2="Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr      "
text3="=============================================================================================="

s/^$/$text1\n$text2\n$text3/ 2.log

where 2.log is:

                                                                                            
           9a            60        36           60.0       60        0            0.0
           96            60        35           58.3       55        0            0.0
           92            60        60           100.0      60        60           100.0
           8e            60        35           58.3       60        0            0.0
                                                                                             
           9a            60        19           31.6       60        0            0.0
           96            60        20           33.3       54        0            0.0
           92            60        60           100.0      60        60           100.0
           8e            60        20           33.3       60        0            0.0

The idea is that i want to add this header in place of each blank line.

Hi Dendany83,

Maybe trying with awk:

awk '{
a="        Device          |             IPMB0-A              |             IPMB0-B              "
b="Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr      "
c="=============================================================================================="
}
{print ($0~/^$/)?(a"\n"b"\n"c):$0}' 2.log

        Device          |             IPMB0-A              |             IPMB0-B              
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr      
==============================================================================================
           9a            60        36           60.0       60        0            0.0
           96            60        35           58.3       55        0            0.0
           92            60        60           100.0      60        60           100.0
           8e            60        35           58.3       60        0            0.0
        Device          |             IPMB0-A              |             IPMB0-B              
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr      
==============================================================================================
           9a            60        19           31.6       60        0            0.0
           96            60        20           33.3       54        0            0.0
           92            60        60           100.0      60        60           100.0
           8e            60        20           33.3       60        0            0.0

If 2.log file contains more than one consecutive blank line and you want to have the same output as above
add cat -s before awk command.

cat -s 2.log | awk '< code >'

Hope this helps.

Regards