2017-10-03 25 views
0

シェルスクリプトを呼び出すMakefielがあります。そのシェルスクリプトの中に私は次のsedコマンドを持っています:シェルスクリプト内でsedを使用して、一致するテキストを別のファイルの内容に置き換えます。

sed -e '/.section\t.text/{' -e 'r anotherfile.s' -e 'd' -e '}' input.s > output.s 

これは動作しません。 これをターミナルに直接実行するとうまく動作します。 ファイル内の ".section .text"行を検索し、別のファイルと置き換えたいとします。 ここで何が間違っていますか?

input.s

.section .data 
    .addressing Word 
_A: 
    .data.32 0 
    .section .text 
    .addressing Word 

_main: 
    LW %GPR27, _C(%GPR0) 
    NOP 

anotherfile.s sedの

.addr_space 32 ; address space is 2^32 
    .addressing Byte ; byte addressing   (default) 
    .bits_per_byte 8 ; 1 byte consists of 8 bit (default) 
    .endian  Big ; Big endian (default) 

    .section .text 
    .org 0x00000000 

output.s(このようにする必要があります)

.section .data 
    .addressing Word 
_A: 
    .data.32 0 
    .addr_space 32 ; address space is 2^32 
    .addressing Byte ; byte addressing   (default) 
    .bits_per_byte 8 ; 1 byte consists of 8 bit (default) 
    .endian  Big ; Big endian (default) 

    .section .text 
    .org 0x00000000 
    .addressing Word 

_main: 
    LW %GPR27, _C(%GPR0) 
    NOP 
+1

[の可能性のある重複バッシュsedのファイルとテキストを置き換えますコンテンツ](https://stackoverflow.com/questions/31056599/bash-sed-replace-text-with-file-content) – Cyrus

+0

親愛なる@Cyrusは、上記のコマンドを端末で使用しています。しかし、シェルスクリプト内でこのコマンドを実行すると、output.sはinput.sと同じです。 – Sajjad

+0

質問するときは、何か「うまくいきません」と言うことはありません。 、エラーメッセージ、コアダンプなどのコードを修正するための助けを求めることは、あなたの車を整備士に持ち帰り、整備士に「うまく行かない」と言って、修理を期待しているようなものです。 –

答えて

0

、S /新しい/古いを行うためのものですはすべてです。それはちょうどのawkを使用し、あなたはsedを使用してそうではなく、あなたが、とにかくやってはならないデバッグ何かしようとすべきではないので、やっていることはありません。

awk ' 
NR==FNR { rep = (rep=="" ? "" : rep ORS) $0; next } 
/\.section\t\.text/ { $0 = rep } 
{ print } 
' anotherfile.s input.s > output.s 
+1

ありがとうございます。出来た! – Sajjad

関連する問題