Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用shell脚本将html文件中的占位符替换为config.json中定义的1-n个特定值 问题是:_Node.js_Regex_Bash_Shell_Perl - Fatal编程技术网

Node.js 使用shell脚本将html文件中的占位符替换为config.json中定义的1-n个特定值 问题是:

Node.js 使用shell脚本将html文件中的占位符替换为config.json中定义的1-n个特定值 问题是:,node.js,regex,bash,shell,perl,Node.js,Regex,Bash,Shell,Perl,我需要用config.json文件中定义的1-n个特定值替换html文件index.html中的占位符(\uuuuuuuu占位符) 我尝试的是: 用新内容替换index.html中的占位符: echo“${html/\\\\\\\\\/$replace}”>index.html 从json获取值(不起作用): replace=$(sed's/*“host”:“\(.*\)”/\1/g;d;t'config.json) 这不是一个好的方法,目前无法返回所需的值 因此,我知道如何替换html文件中的

我需要用config.json文件中定义的1-n个特定值替换html文件index.html中的占位符(
\uuuuuuuu占位符

我尝试的是: 用新内容替换index.html中的占位符:
echo“${html/\\\\\\\\\/$replace}”>index.html

从json获取值(不起作用):
replace=$(sed's/*“host”:“\(.*\)”/\1/g;d;t'config.json)
这不是一个好的方法,目前无法返回所需的值

因此,我知道如何替换html文件中的值,但在此之前,我不知道如何将所需的值放入变量中。我已经用sed或perl试过了,但是sed的问题是它不需要跨平台兼容

config.json: default和auth只是一些可能的值,它可以是1-n个主机

最后,我想要一个所有主机都是字符串的变量:host1 host2 host3

信息:我们不能使用系统上通常不存在的任何第三方工具&index.html文件不感兴趣,我们只是替换占位符值

我感谢你的帮助

我们不能使用系统上通常不存在的任何第三方工具

考虑到我们不知道您使用的是什么“系统”,这并不是一个非常有用的限制

Perl有几个可用的JSON解析器,自Perl5.14(于2011年5月发布)以来,其中一个()已成为标准Perl发行版的一部分。因此,如果您有一个在过去九年中发布的Perl版本,那么将JSON文件读入变量的任务是微不足道的

#!/user/bin/perl

use strict;
use warnings;
use feature 'say';

use JSON::PP;
use Data::Dumper;

open my $json_fh, '<', 'config.json' or die $!;

my $json = do { local $/; <$json_fh> };

my $config = JSON::PP->new->decode($json);

# Now you have your JSON in a Perl data structure
say Dumper $config;

# You can also access individual values
say $config->{xxxx}{description};
say $config->{API}{value}{default}{host};
#/user/bin/perl
严格使用;
使用警告;
使用特征“说”;
使用JSON::PP;
使用数据::转储程序;

打开我的$json_fh,'你们说你们不能使用系统中通常不存在的第三方工具,但现在你们已经用node.js标记了。您没有告诉我们您通常的系统是什么样子,或者您认为第三方是什么。例如,在Perl中,这很容易在将Perl5.14开箱即用的系统中实现,因为它在核心中包含一个JSON解析器。几乎所有在过去10年中发布的Linux发行版都将拥有它。您必须编写少于10行的代码。但是你对你的要求不是特别清楚,所以我恐怕帮不了你。你能给我们一个(小)样本输入文件以及你期望的输出吗?你能把
{API}{value}{default}{host}
作为你脚本的参数提交吗?@Cyrus:,您可以使用
eval
使各种怪物永久化,但我真的不推荐这样做。最好传入需要使用的键的表示形式——可能是“API:value:default:host”,然后编写代码对其进行解析,并使用该信息在散列中查找数据。
#!/user/bin/perl

use strict;
use warnings;
use feature 'say';

use JSON::PP;
use Data::Dumper;

open my $json_fh, '<', 'config.json' or die $!;

my $json = do { local $/; <$json_fh> };

my $config = JSON::PP->new->decode($json);

# Now you have your JSON in a Perl data structure
say Dumper $config;

# You can also access individual values
say $config->{xxxx}{description};
say $config->{API}{value}{default}{host};