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
1 Like
ygemici:
# 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 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
regards
ygemici
ygemici:
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
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/;
cgkmal
April 12, 2011, 5:21am
9
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
cgkmal:
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.
dendany83:
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/;
i think you run in solaris or aix or other Unix variants.
if we suppose your system as solaris or other , sed has some limits unfortunately and does not understand what is newline in its..
if we coold run successfully this, results are useless again.because do not make sense for SED in some systems which i mentioned above.
#sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' header
if you try this in spite of everything for learning this command's output you can try like this (is same but no error )
# sed -e ':a' -e 'N;$!ba' -e 's/\n/\\n/g' header
However we must a find a solution for Sed Lovers
I use test four lines in text (as your header) file..
# cat text
Device | IPMB0-A | IPMB0-B
Board Address |Sent SentErr %Errr |Sent SentErr %Errr
Board1111 Address |Sent SentErr %Errr |Sent SentErr %Errr
Board1212121 Address |Sent SentErr %Errr |Sent SentErr %Errr
# cat infile
begin this is
this is ...................
that is.................
begin this is
I use the file named text that has contents to add in our infile
main code is below..if you want you can use for as in script file.
xdel=() ; xdel=($(sed -n '/^begin/=' infile))
y=0 ; x=() ; while IFS='\n' read -r line; do x=("${x[@]}" "$line"); ((y++)) ; done <text
cp infile infileorg ; loop=notok;loopinc=notok;wh=1;for i in ${xdel[@]} ; do for j in "${x[@]}" ;do if [ $wh = 1 ] ; then c=c ; else c=a ; fi ;
if (( $wh > 2 )) ; then ((i++)); fi;if [ $loop = ok ] && [ $loopinc != ok ] ; then (( i = i + y - 1 )) ;loopinc=ok; fi ; sed ''$i' '$c'\
>'"$j"' ' infile>infiletmp ;mv infiletmp infile;((wh++)); done ; wh=1 ;loop=ok ; done;
echo -e "\n===== Org File(infileorg) =====\n\n$(cat infileorg)\n\n ===== Output File(infile) =====\n\n$(cat infile)\n"
===== Org File(infileorg) =====
begin this is
this is ...................
that is.................
begin this is
===== Output File(infile) =====
Device | IPMB0-A | IPMB0-B
Board Address |Sent SentErr %Errr |Sent SentErr %Errr
Board1111 Address |Sent SentErr %Errr |Sent SentErr %Errr
Board1212121 Address |Sent SentErr %Errr |Sent SentErr %Errr
this is ...................
that is.................
Device | IPMB0-A | IPMB0-B
Board Address |Sent SentErr %Errr |Sent SentErr %Errr
Board1111 Address |Sent SentErr %Errr |Sent SentErr %Errr
Board1212121 Address |Sent SentErr %Errr |Sent SentErr %Errr
regards
ygemici
[quote=ygemici;302513572]
i think you run in solaris or aix or other Unix variants.
if we suppose your system as solaris or other , sed has some limits unfortunately and does not understand what is newline in its..
if we coold run successfully this, results are useless again.because do not make sense for SED in some systems which i mentioned above.
#sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' header
if you try this in spite of everything for learning this command's output you can try like this (is same but no error )
# sed -e ':a' -e 'N;$!ba' -e 's/\n/\\n/g' header
However we must a find a solution for Sed Lovers
Hi ygemici,
Yes, you are absolutely right, it is working fine on Cygwin but not on Solaris! Now i'm doing my tests on Linux platform (target platform for my project) and it is showing the below result (no errors but the o/p is not fully OK):
# sed "s/^$/$(sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' header)/" file1.log
Device | IPMB0-A | IPMB0-BnBoard Address |Sent SentErr %Errr |Sent SentErr %Errr n============================================================================================
Board1;9a;60;36;60.0;60;0;0.0
Board2;96;60;35;58.3;55;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;35;58.3;60;0;0.0
Device | IPMB0-A | IPMB0-BnBoard Address |Sent SentErr %Errr |Sent SentErr %Errr n============================================================================================
Board1;9a;60;19;31.6;60;0;0.0
Board2;96;60;20;33.3;54;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;20;33.3;60;0;0.0
#sed "s/^$/$(sed -e ':a' -e 'N;$!ba' -e 's/\n/\\n/g' header)/" file1.log
Device | IPMB0-A | IPMB0-BnBoard Address |Sent SentErr %Errr |Sent SentErr %Errr n============================================================================================
Board1;9a;60;36;60.0;60;0;0.0
Board2;96;60;35;58.3;55;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;35;58.3;60;0;0.0
Device | IPMB0-A | IPMB0-BnBoard Address |Sent SentErr %Errr |Sent SentErr %Errr n============================================================================================
Board1;9a;60;19;31.6;60;0;0.0
Board2;96;60;20;33.3;54;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;20;33.3;60;0;0.0
is there any WA? or it is the same problem of \n? this is the only blocking problem to finalize my script
As for the last code you have mentioned, this is completely far from my knowledge it will take time for me to forget that