AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / server / Perguntas / 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

Como substituir uma string de um resultado awk?

  • 772

Eu tenho um arquivo JSON que contém várias ocorrências de replaceme.

Eu só quero substituir uma ocorrência com base em uma string específica encontrada algumas linhas acima.

Por exemplo,

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

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

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

Neste exemplo, quero substituir replacemeapenas por title2.

Com awk 'title2/,/replaceme/' myFileisso, consigo localizar a parte do meu arquivo JSON. Preciso executar um comando sed também, mas não sei como, ou se é possível dessa forma?

sed
  • 1 1 respostas
  • 665 Views

1 respostas

  • 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"
        }
    

    Fontes

    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

relate perguntas

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    Você pode passar usuário/passar para autenticação básica HTTP em parâmetros de URL?

    • 5 respostas
  • Marko Smith

    Ping uma porta específica

    • 18 respostas
  • Marko Smith

    Verifique se a porta está aberta ou fechada em um servidor Linux?

    • 7 respostas
  • Marko Smith

    Como automatizar o login SSH com senha?

    • 10 respostas
  • Marko Smith

    Como posso dizer ao Git para Windows onde encontrar minha chave RSA privada?

    • 30 respostas
  • Marko Smith

    Qual é o nome de usuário/senha de superusuário padrão para postgres após uma nova instalação?

    • 5 respostas
  • Marko Smith

    Qual porta o SFTP usa?

    • 6 respostas
  • Marko Smith

    Linha de comando para listar usuários em um grupo do Windows Active Directory?

    • 9 respostas
  • Marko Smith

    O que é um arquivo Pem e como ele difere de outros formatos de arquivo de chave gerada pelo OpenSSL?

    • 3 respostas
  • Marko Smith

    Como determinar se uma variável bash está vazia?

    • 15 respostas
  • Martin Hope
    Davie Ping uma porta específica 2009-10-09 01:57:50 +0800 CST
  • Martin Hope
    kernel O scp pode copiar diretórios recursivamente? 2011-04-29 20:24:45 +0800 CST
  • Martin Hope
    Robert ssh retorna "Proprietário incorreto ou permissões em ~/.ssh/config" 2011-03-30 10:15:48 +0800 CST
  • Martin Hope
    Eonil Como automatizar o login SSH com senha? 2011-03-02 03:07:12 +0800 CST
  • Martin Hope
    gunwin Como lidar com um servidor comprometido? 2011-01-03 13:31:27 +0800 CST
  • Martin Hope
    Tom Feiner Como posso classificar a saída du -h por tamanho 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich O que é um arquivo Pem e como ele difere de outros formatos de arquivo de chave gerada pelo OpenSSL? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent Como determinar se uma variável bash está vazia? 2009-05-13 09:54:48 +0800 CST

Hot tag

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

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve