n番目のパターンマッチのための内部ファイルの置き換え

n番目のパターンマッチのための内部ファイルの置き換え

行が多いファイルがありますが、要件を次のように要約できます。

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

2番目の発生後に新しい行を挿入し、上記と同様の別の変数行を使用し、3番目の行が一致した後も同じです。

次のようなさまざまな組み合わせを試しましたが、うまくいきません。

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を使用してすべてを交換できますが、個別には不可能です。 1つのタスクがすべてを置き換えます。

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

変数に保存する代わりに、すべての出力(1行に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.

******/フィニー。

おすすめ記事