AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 644375
Accepted
Bastien974
Bastien974
Asked: 2014-11-15 06:49:45 +0800 CST2014-11-15 06:49:45 +0800 CST 2014-11-15 06:49:45 +0800 CST

如何从 awk 结果中替换字符串?

  • 772

我有一个 JSON 文件,其中包含多次出现的replaceme.

我只想根据上面几行找到的特定字符串替换一个事件。

例如,

[...]
    "title" : {
      "Propertie1" : "xxxx",
      "Propertie2: "replaceme"
    }

    "title2" : {
      "Propertie1" : "xxxx",
      "Propertie2: "replaceme"
    }
    "title" : {
      "Propertie1" : "xxxx",
      "Propertie2: "replaceme"
    }

    "title2" : {
      "Propertie1" : "xxxx",
      "Propertie2: "replaceme"
    }
[...]

在此示例中,我只想replaceme替换title2.

我awk 'title2/,/replaceme/' myFile能够找到我的 JSON 文件的一部分。我是否也需要执行 sed 命令,但我不知道如何,或者是否有可能这样做?

sed
  • 1 1 个回答
  • 665 Views

1 个回答

  • Voted
  1. Best Answer
    030
    2014-11-15T11:34:14+08:002014-11-15T11:34:14+08:00
    [vagrant@localhost ~]$ sed -e '/"title2/,/}/{ s/replaceme/title2/; }' test
        "title" : {
          "Propertie1" : "xxxx",
          "Propertie2: "replaceme"
        }
    
        "title2" : {
          "Propertie1" : "xxxx",
          "Propertie2: "title2"
        }
    
        "title" : {
          "Propertie1" : "xxxx",
          "Propertie2: "replaceme"
        }
    
        "title2" : {
          "Propertie1" : "xxxx",
          "Propertie2: "title2"
        }
    

    来源

    https://stackoverflow.com/questions/268045/multi-line-search-and-replace-tool

    http://sed.sourceforge.net/sedfaq4.html#s4.23.3

    4.24. How do I address all the lines between RE1 and RE2, excluding the lines themselves?
    
    Normally, to address the lines between two regular expressions, RE1 and RE2, one would do this: '/RE1/,/RE2/{commands;}'. Excluding those lines takes an extra step. To put 2 arrows before each line between RE1 and RE2, except for those lines:
    
         sed '1,/RE1/!{ /RE2/,/RE1/!s/^/>>/; }' input.fil
    
    The preceding script, though short, may be difficult to follow. It also requires that /RE1/ cannot occur on the first line of the input file. The following script, though it's not a one-liner, is easier to read and it permits /RE1/ to appear on the first line:
    
         # sed script to replace all lines between /RE1/ and /RE2/,
         # without matching /RE1/ or /RE2/
         /RE1/,/RE2/{
           /RE1/b
           /RE2/b
           s/^/>>/
         }
         #---end of script---
    
    Contents of input.fil: Output of sed script:
          aaa                           aaa
          bbb                           bbb
          RE1                           RE1
          aaa                           >>aaa
          bbb                           >>bbb
          ccc                           >>ccc
          RE2                           RE2
          end                           end
    
    • 1

相关问题

  • 如何使用 sed 或 grep 从文件中抓取单词

  • 从文件中读取指定范围的行

  • 如何使用 grep 查找日志文件中最常出现的错误?

  • SED 查找和替换 w\ 正则表达式

  • 在从 linux 生成的电子邮件中获取正确的换行符

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve