Home > Software design >  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?

Time:11-15

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.

CodePudding user response:

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)

CodePudding user response:

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

  • Related