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

337
Vistas
How to remove a lines if a line contains empty value after = symbol using powershell or Javascript?

How to remove a lines if a line contains empty value after = symbol using powershell or Javascript?

My input value is

variable1 = value1
variable2 = value2
variable3 =
variable4 = value4
variable5 = 

the values may change .i am looking for a script that remove lines if the value is empty after = symbol.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It looks like you are trying to alter an .INI file and by doing so, you may in fact ruin some applications initialization..

Having said that, in PowerShell you can do:

  1. when loading from a text file
$result = Get-Content -Path 'X:\theInputFile.txt' | Where-Object { $_ -match '^\w+\s*=\s*\S+' }
  1. when parsing a multiline string
$string = @'
variable1 = value1
variable2 = value2
variable3 =
variable4 = value4
variable5 = 
'@ 

$result = $string -split '\r?\n' | Where-Object { $_ -match '^\w+\s*=\s*\S+' }

Variable $result will be an array of strings:

variable1 = value1
variable2 = value2
variable4 = value4

You can save that back to (a new) file using

$result | Set-Content -Path 'X:\theNewInputFile.txt'

Regex details:

^          Assert position at the beginning of the string
\w         Match a single character that is a “word character” (letters, digits, etc.)
   +       Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s         Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   *       Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
=          Match the character “=” literally
\s         Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   *       Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\S         Match a single character that is a “non-whitespace character”
   +       Between one and unlimited times, as many times as possible, giving back as needed (greedy)
about 4 years ago · Juan Pablo Isaza Denunciar

0

To complement Theo's helpful answer, which uses a streaming, cmdlet-based approach, with a (pure) operator approach, which is faster, but more memory-intensive (memory use is typically not a concern with text files):

As zett42 points out, comparison operators such as -match act as filters if the LHS operand (input) is an array, so you can match a regex directly against an array of lines in order to get the sub-array of matching lines:

# Filter the lines of file inputFile.txt by returning only those on which
# "=" is followed by at least one non-whitespace character ("\S"), 
# optionally preceded by any number of other characters (".*").
@(Get-Content inputFile.txt) -match '=.*\S'

You can speed up the Get-Content call by adding -ReadCount 0.

As for when to use a streaming vs. a pure operator approach: see the section about tradeoffs in this answer.

about 4 years ago · Juan Pablo Isaza 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