我有一个包含很多行的文件,但我可以总结我的要求,如下所示:
<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>
我必须在第一次出现后插入新行:
<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>
在第二次出现后插入一个新行,并使用与上面类似的另一个变量行,在第三行匹配后也是如此。
我已经尝试过类似的各种组合,但它不起作用:
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
我可以使用 sed 替换所有内容,但不能单独替换。工作一个取代所有:
sed -i.bak "s#$input1#$input1\n\t\t$output1#" 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: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>
答案1
不要存储在变量中,而是将所有输出(每行一个)放在一个文件中,例如 outf 然后执行以下操作:
$ sed -e "\\#${input1}#R outf" a
这假设您使用的是 GNU sed。一旦您对更改感到满意,请引入 -i 选项。
解释:
° Alternate delimiter needs to escaped by a backslash. In our case, # is the delimiter, so we escape it with a backslash.
° Wait a minute but I see two of them here, you'll be asking. Well coz, before what we write on the command line gets to sed is seen n processed by the shell. So since backslash is special within double quotes, we need to double it so that after shell has seen it, it shall be presented as one to sed. So the Universe is happy.
° Now comes the R command. This is a nonstandard command given by Gnu folks, and is precisely what the doctor ordered for your scenario. It will print one line each time it is invoked and place that line after the current input line is printed.
****** /菲尼。