sed-文字列をファイルの内容に置き換えます


21

2つのファイルがあります:file1file2

file1 次の内容があります。

---
  host: "localhost"
  port: 3000
  reporter_type: "zookeeper"
  zk_hosts: 
    - "localhost:2181"

file2IPアドレスが含まれています(1.1.1.1

私がしたいのは、に置き換えlocalhost1.1.1.1、最終結果が次のようになることです:

---
  host: "1.1.1.1"
  port: 3000
  reporter_type: "zookeeper"
  zk_hosts: 
    - "1.1.1.1:2181"

私が試してみました:

sed -i -e "/localhost/r file2" -e "/localhost/d" file1
sed '/localhost/r file2' file1 |sed '/localhost/d'
sed -e '/localhost/r file2' -e "s///" file1

ただし、行全体を置き換えるか、変更する必要があるIPの後に行にIPを移動します。


1
確かではありませんが、cat file1 | sed -e 's/localhost/1.1.1.1/g'動作しますか?
dchirikov

1
\rsedコマンドを見てください。
ケビン14

回答:


14

ここでsed解決策は:

% sed -e "s/localhost/$(sed 's:/:\\/:g' file2)/" file1
---
  host: "1.1.1.1"
  port: 3000
  reporter_type: "zookeeper"
  zk_hosts: 
    - "1.1.1.1:2181"

sed -i変更をその場で行うために使用する必要があります。

を使用できる場合awk、これを行う1つの方法を次に示します。

% awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
---
  host: "1.1.1.1"
  port: 3000
  reporter_type: "zookeeper"
  zk_hosts: 
    - "1.1.1.1:2181"

4
+1 awk。これsed できると思いますが、ひどく不器用です。これがawk輝くところです!
HalosGhost

1
@HalosGhost:それは私とあなたがOPの質問を誤解していたようです、私は私の答えを更新しました。
cuonglm 14

sedファイルにスペースまたはグロブ文字が含まれている場合、ソリューションのコマンド置換は二重引用符で囲む必要があります。
グレアム14

@Graeme:ありがとう、更新されました!自由に編集してください。
cuonglm

2
あなたは両方をエスケープする必要があります/し、&代わりに。それは"$(sed 's:[/\\&]:\\&:g' file2)"
トビースペイト

6

sed使用する前に、シェルコマンド置換を使用して、置換文字列でファイルを読み取ることができます。したがってsed、通常の置換のみが表示されます。

sed "s/localhost/$(cat file2)/" file1 > changed.txt


18
これは、複数行のファイルおよび特殊文字を含むファイルで機能しますか?
トレバーヒッキー

9
@TrevorHickeyではありません。Sedは、「未終了の「s」コマンド」エラーで失敗します。
DarioP

1

使用してみてください

join file1 file2

次に、不要なフィールドを削除します。


1

今日、この「問題」もありました。テキストのブロックを別のファイルの内容に置き換える方法です。

(スクリプトで再利用できる)bash関数を作成することで解決しました。

[cent@pcmk-1 tmp]$ cat the_function.sh
# This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
# The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
#
# Usage:
#    seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
function substitute_BLOCK_with_FILEcontents {
  local BLOCK_StartRegexp="${1}"
  local BLOCK_EndRegexp="${2}"
  local FILE="${3}"
  sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
}

[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ source the_function.sh
[cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/result.txt
100
101
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
105
106
107
108
109
110
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.