加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell – 查找单词并替换为文件中的单词

发布时间:2020-12-16 01:49:23 所属栏目:安全 来源:网络整理
导读:我的任务是创建配置文件.配置文件是 JSON文件.以下示例 "text": { "data": "Click Here","size": 36,"style": "bold","name": "text1","hOffset": 250,"vOffset": 100,"alignment": "center","onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" } 配
我的任务是创建配置文件.配置文件是 JSON文件.以下示例

"text": {
        "data": "Click Here","size": 36,"style": "bold","name": "text1","hOffset": 250,"vOffset": 100,"alignment": "center","onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }

配置会随着json的“style”属性的改变而重复.所以我要复制上面的文字说50次.然后我想用另一个逗号分隔文件输入的不同样式替换“style”属性,如下所示.

bold,italic,cryptic

那么输出就是

"text": {
            "data": "Click Here","onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
        }
"text": {
            "data": "Click Here","style": "italic","style": "cryptic","onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
        }

这样做有更简单的方法吗?

解决方法

使用GNU awk进行gensub的另一个解决方案:

awk -v RS= '
  NR==FNR { n = split($0,styles,/,/); next } 
  {
    for (i=1; i<=n; i++) {
      style = """ styles[i] """
      print gensub (/(["]style["]: ).[^,]+/,"1" style,"G",$0); 
    }
  }
' style.txt config.file

首先使用名为style.txt的文件,该文件具有所需样式的逗号分隔值.您将它们存储在一个数组中.您继续迭代配置文件并换出样式段并将其替换为新模式.

我不愿意承认使用Perl,Ruby或python的JSON解析器可以更好地解析JSON,但是由于您没有对配置文件的任何控制,因此将其作为一次性选项发布.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读