I have the following JSON input
... "somefield":"somevalue", "time":"timevalue", "anotherfield":"value" ...
我正在我的 KornShell (ksh
) 脚本中,希望用我的值替换 timevalue。因此,我创建了这个正则表达式,使用组来工作得很好。
data=`cat somefile.json`
echo $data | perl -pe "s|(.*time\"\s*\:\s*\").*?(\".*)|\1%TIME%\2|g" | another-script.sh
... "somefield":"somevalue", "time":"%TIME%", "anotherfield":"value" ...
然而,我不能使用数字作为替换,因为Perl使用数字来定义组,所以这个显然不起作用:
perl -pe "s|(.*time\"\s*\:\s*\").*?(\".*)|\120:00:00\2|g"
我可以通过进行两步替换来解决这个问题,
perl -pe "s|(.*time\"\s*\:\s*\").*?(\".*)|\1%TIME%\2|g" | perl -pe "s|%TIME%|20:00:00|"
... "somefield":"somevalue", "time":"20:00:00", "anotherfield":"value" ...
但我相信有更好、更优雅的方法来实现它。