簡體   English   中英

在 Linux 中的 sed 命令中執行 cat 命令

[英]Execute a cat command within a sed command in Linux

我有一個包含一些內容的file.txt 我想在file1.txt搜索一個字符串,如果該字符串匹配,我想用file.txt的內容替換該字符串。 我怎樣才能做到這一點?

我試過使用sed

sed -e 's/%d/cat file.txt/g' file1.txt

這是在file1.txt搜索匹配的字符串並將其替換為字符串cat file.txt ,但我想要file.txt內容。

在將文件插入到 sed 字符串之前,如何將文件的內容保存在變量中?

$content=`cat file.txt`; sed "s/%d/${content}/g file1.txt"

給定帶有內容的original.txt

this is some text that needs replacement

和帶有內容的replacement.txt

no changes

我不確定您的替換標准是什么,但假設我想替換原始文件中所有出現的單詞replacement ,您可以使用其他文件的內容替換原始文本的內容:

> sed -e 's/replacement/'"`cat replacement.txt`"'/g' original.txt
this is some text that needs no changes

您可以使用sed使用r命令讀取文件。 但是,這是基於行的操作,這可能不是您所追求的。

sed '/%d/r file1.txt'

讀取發生在“每行”循環結束時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM