Sed 用文件内容替换单个文本

问题描述 投票:0回答:1

我对使用 sed 有疑问。

我有 json 文件,我想在文件的我的部分之一中进行替换。

所以我正在尝试获取包含 4500 行的 .json 文件的所有内容。并替换我的 HTML 中的特定文本:

{DATA_INPUT}

我一直在用

DATA_JSON=$(echo "$(<data.json)")

sed -i "s/{DATA_INPUT}]{\}/${DATA_JSON}{\}/g" convert.html

但似乎不起作用,

我越来越

/bin/find: Argument list too long

我也尝试过:

DATA_JSON=$(echo "$(<data.json)")
find . -maxdepth 1 -type f -name 'convert.html' \
    -exec sed -n "s/{DATA_INPUT}/${DATA_JSON}/g" {} \;

但是得到:

 /bin/sed: Argument list too long

我正在尝试将其与 Bash 集成

所以基本上我试图从文件中获取所有内容并替换为convert.html中的文本

bash shell sed find
1个回答
0
投票

sed
已经知道如何插入文件的内容。

sed -i "/{DATA_INPUT}/r data.json" convert.html

单独而言,这将在匹配后的以下行中插入

data.json
的内容。如果你也想操纵比赛,也许可以尝试类似的事情

sed "/{DATA_INPUT}/!b;s///;x;r data.json;x" convert.html

(可能会省略

-i
,直到您对自己的工作脚本感到满意为止)或尝试使用例如Awk 或 Perl,只读性稍差;

awk 'NR==FNR { array = array (array ? "\n" : "") $0; next }
    /\{DATA_INPUT\}\{\}/ { sub(/\{DATA_INPUT\}/, array) }
   1' data.json convert.html
© www.soinside.com 2019 - 2024. All rights reserved.