如何在Linux中的某些特定标签后替换单词

时间:2019-05-28 10:21:30

标签: linux shell

我处于一种情况,在找到匹配项后,我需要替换一些特定的单词。 条件是标记应以“ detailSelection”开头,此后无论哪里有WORD1,我都需要将其替换为WORD2。

这是一个大文件的一小段,我必须成百上千个文件来做。

                         "detailSelection": {
                                        "name": "Alarm Detail",
                                        "type": "form-operation",
                                        "requireSelection": true,
                                        "dataSelection": {
                                            "typeSelection": {
                                                "domain": "ossm",
                                                "package": "WORD1",
                                                "type": "WORD1"
                                            },

输出应类似于:

                   "detailSelection": {
                                        "name": "Alarm Detail",
                                        "type": "form-operation",
                                        "requireSelection": true,
                                        "dataSelection": {
                                            "typeSelection": {
                                                "domain": "ossm",
                                                "package": "WORD2",
                                                "type": "WORD2"
                                            },

1 个答案:

答案 0 :(得分:0)

非常简单,您需要使用sed命令

sed -i 's/WORD1/WORD2/g' filename.txt

-i将在文件内部执行此替换。换句话说,它将更改文件。如果您要创建一个新文件,那么

sed 's/WORD1/WORD2/g' filename.txt > newfile.txt