A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.
4 added 1055 characters in body
source | link

Expected output:

            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:2323/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>
            <DT><A HREF="http://127.0.0.1:5678/FGHIJ/wp-admin/index.cfm?event&msg=secure&fr=sp">abc (test)</A>
--
            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:2324/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>
            <DT><A HREF="http://127.0.0.1:8303/CFIDE/administrator/index.cfm?event&msg=secure&fr=sp">xyz (Prod)</A>
--
            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:2124/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>
            <DT><A HREF="http://127.0.0.1:8303/CFIDE/administrator/index.cfm?event&msg=secure&fr=sp">lmn (Prod)</A>

Expected output:

            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:2323/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>
            <DT><A HREF="http://127.0.0.1:5678/FGHIJ/wp-admin/index.cfm?event&msg=secure&fr=sp">abc (test)</A>
--
            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:2324/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>
            <DT><A HREF="http://127.0.0.1:8303/CFIDE/administrator/index.cfm?event&msg=secure&fr=sp">xyz (Prod)</A>
--
            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:2124/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>
            <DT><A HREF="http://127.0.0.1:8303/CFIDE/administrator/index.cfm?event&msg=secure&fr=sp">lmn (Prod)</A>
3 Bash isn't a text editor
| link

Bash inplace file replacement for nth pattern match

2 added 402 characters in body
source | link

I have a file with many lines but I can summarise my requirements like below:

            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm">Wonderlandcfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:5678/FGHIJ/wp-admin/index.cfm">abccfm?event&msg=secure&fr=sp">abc (test)</A>
--
            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm">Wonderlandcfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:8303/CFIDE/administrator/index.cfm">xyzcfm?event&msg=secure&fr=sp">xyz (Prod)</A>
--
            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm">Wonderlandcfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:8303/CFIDE/administrator/index.cfm">lmncfm?event&msg=secure&fr=sp">lmn (Prod)</A>

I have to insert a new line after first occurrence of:

<DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm">Wonderlandcfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>

similar to it e.g.

<DT><A HREF="http://127.0.0.1:2323/xnmp/wp-admin/index.cfm">NewSitecfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>

insert a new line after second occurrence with another variable line similar to above and so does after third line match.

I have tried something like this with various combinations but it is not working:

input1='<DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm">Wonderlandcfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
output1='<DT><A HREF="http://127.0.0.1:2323/xnmp/wp-admin/index.cfm">NewSitecfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>'
output2='similaroutput2='<DT><A toHREF="http://127.0.0.1:2324/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site above'4)</A>'
output3='similaroutput3='<DT><A toHREF="http://127.0.0.1:2124/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site above'4)</A>'
gawk -i inplace -v in2="$input1" -v voutput1="$output1" '/in2/{c++;if(c==1){sub(in2,in2 "\n" voutput3);c=0}}1' a  ; where a is my file name

I am able to replace all using sed but not individually. Working one which replaces all:

sed -i.bak "s#$input1#$input1\n\t\t$output1#" a

I have a file with many lines but I can summarise my requirements like below:

            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:5678/FGHIJ/wp-admin/index.cfm">abc (test)</A>
--
            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:8303/CFIDE/administrator/index.cfm">xyz (Prod)</A>
--
            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:8303/CFIDE/administrator/index.cfm">lmn (Prod)</A>

I have to insert a new line after first occurrence of:

<DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm">Wonderland(Site 3)</A>

similar to it e.g.

<DT><A HREF="http://127.0.0.1:2323/xnmp/wp-admin/index.cfm">NewSite(Site 4)</A>

insert a new line after second occurrence with another variable line similar to above and so does after third line match.

I have tried something like this with various combinations but it is not working:

input1='<DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm">Wonderland(Site 3)</A>
output1='<DT><A HREF="http://127.0.0.1:2323/xnmp/wp-admin/index.cfm">NewSite(Site 4)</A>'
output2='similar to above'
output3='similar to above'
gawk -i inplace -v in2="$input1" -v voutput1="$output1" '/in2/{c++;if(c==1){sub(in2,in2 "\n" voutput3);c=0}}1' a  ; where a is my file name

I am able to replace all using sed but not individually. Working one which replaces all:

sed -i.bak "s#$input1#$input1\n\t\t$output1#" a

I have a file with many lines but I can summarise my requirements like below:

            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:5678/FGHIJ/wp-admin/index.cfm?event&msg=secure&fr=sp">abc (test)</A>
--
            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:8303/CFIDE/administrator/index.cfm?event&msg=secure&fr=sp">xyz (Prod)</A>
--
            <DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
            <DT><A HREF="http://127.0.0.1:8303/CFIDE/administrator/index.cfm?event&msg=secure&fr=sp">lmn (Prod)</A>

I have to insert a new line after first occurrence of:

<DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>

similar to it e.g.

<DT><A HREF="http://127.0.0.1:2323/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>

insert a new line after second occurrence with another variable line similar to above and so does after third line match.

I have tried something like this with various combinations but it is not working:

input1='<DT><A HREF="http://127.0.0.1:1234/ABCDE/wp-admin/index.cfm?event&msg=secure&fr=sp">Wonderland(Site 3)</A>
output1='<DT><A HREF="http://127.0.0.1:2323/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>'
output2='<DT><A HREF="http://127.0.0.1:2324/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>'
output3='<DT><A HREF="http://127.0.0.1:2124/xnmp/wp-admin/index.cfm?event&msg=secure&fr=sp">NewSite(Site 4)</A>'
gawk -i inplace -v in2="$input1" -v voutput1="$output1" '/in2/{c++;if(c==1){sub(in2,in2 "\n" voutput3);c=0}}1' a  ; where a is my file name

I am able to replace all using sed but not individually. Working one which replaces all:

sed -i.bak "s#$input1#$input1\n\t\t$output1#" a
1
source | link