Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

344
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!