Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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
Shell 找到一个单词并替换为文件中的单词_Shell_Awk_Sed_Vi_Replacewith - Fatal编程技术网

Shell 找到一个单词并替换为文件中的单词

Shell 找到一个单词并替换为文件中的单词,shell,awk,sed,vi,replacewith,Shell,Awk,Sed,Vi,Replacewith,我的任务是创建一个配置文件。配置文件是一个JSON文件。下面的例子 "text": { "data": "Click Here", "size": 36, "style": "bold", "name": "text1", "hOffset": 250, "vOffset": 100, "alignment": "center", "onMouseUp": "sun1.

我的任务是创建一个配置文件。配置文件是一个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",
            "size": 36,
            "style": "bold",
            "name": "text1",
            "hOffset": 250,
            "vOffset": 100,
            "alignment": "center",
            "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
        }
"text": {
            "data": "Click Here",
            "size": 36,
            "style": "italic",
            "name": "text1",
            "hOffset": 250,
            "vOffset": 100,
            "alignment": "center",
            "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
        }
"text": {
            "data": "Click Here",
            "size": 36,
            "style": "cryptic",
            "name": "text1",
            "hOffset": 250,
            "vOffset": 100,
            "alignment": "center",
            "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
        }

有没有更简单的方法可以做到这一点?

我不确定您想应用它,但您可以在每次迭代中读取相同的json,并在迭代中根据需要更改样式的值。只是个主意。

awk
救命

$ awk -v RS= 'NR==FNR{t=$0;next} 
                     {n=split($0,styles,","); 
                      for(i=1;i<=n;i++) {s=t; 
                                         sub("bold",styles[i],s); 
                                         print s
                                        }
                     }' template styles
$awk-vrs='NR==FNR{t=$0;next}
{n=拆分($0,样式,“,”);

对于(i=1;i另一种使用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
awk-v RS='1〕
NR==FNR{n=split($0,styles,/,/);next}
{

对于(i=1;iReading of json超出了我的控制范围。我只提供了配置文件。因此我必须在文件中执行此操作。如果您可以只修改json文件,那么我认为您可以做的事情不多。请等待,看看是否有其他人知道方法。如果您有权在文件之外修改文件(在代码中)您可以简单地使用put方法g.JSONObject date=jsonArray.getJSONObject(0.getJSONObject(“日”);person.put(“日”、“周二”);