Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

708
Vistas
Usando PowerShell cómo reemplazar y guardar un valor en un archivo json con comentarios

Tengo un archivo Json con varios comentarios y quiero reemplazar un valor en él.

Intenté lo siguiente y me da un archivo json sin comentarios. Pero no entiendo cómo cambiar el valor y guardarlo con comentarios. ¿Es esto posible porque estamos reemplazando todos los comentarios con líneas vacías?

 $json = Get-Content $jsonfile -Raw | ConvertFrom-Json $configfile = $json -replace '(?m)(?<=^([^"]|"[^"]*")*)//.*' -replace '(?ms)/\*.*?\*/'

Mi config.json es el siguiente y quiero cambiar el valor de "versión" a 10 a través de un script de PowerShell y luego guardarlo con los comentarios intactos.

 { "FramewokSettings": { "Name": "VX", "Version": "8", // The value here is not constant. It can be something like v1.4.56.456 also "GitVersion": "v5", "DatabaseVersion": "7", // Doing xyz "CounterVersion": "2" // Doing ABC. // Start } }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Utilizar

 (Get-Content test.txt) -replace '^(\s*"Version"\s*:\s*")[^"]*', '${1}10' | Set-Content test.txt

Ver prueba .

EXPLICACIÓN

 -------------------------------------------------------------------------------- ^ the beginning of the string -------------------------------------------------------------------------------- ( group and capture to $1: -------------------------------------------------------------------------------- \s* whitespace (0 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- "Version" '"Version"' -------------------------------------------------------------------------------- \s* whitespace (0 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- : ':' -------------------------------------------------------------------------------- \s* whitespace (0 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- " '"' -------------------------------------------------------------------------------- ) end of $1 -------------------------------------------------------------------------------- [^"]* any character except: '"' (0 or more times (matching the most amount possible))
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda