首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果一行包含使用powershell或Javascript的=符号后的空值,如何删除行?

如果一行包含使用powershell或Javascript的=符号后的空值,如何删除行?
EN

Stack Overflow用户
提问于 2021-11-14 12:37:50
回答 1查看 69关注 0票数 0

如果一行包含使用powershell或Javascript的=符号后的空值,如何删除行?

我的输入值是

代码语言:javascript
运行
复制
variable1 = value1
variable2 = value2
variable3 =
variable4 = value4
variable5 = 

这些值可能会改变.i am,以寻找一个脚本,如果该值是空的,请在==符号之后删除行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-14 13:13:37

看起来您正在尝试修改.INI文件,这样做实际上可能会破坏一些应用程序的初始化。

话虽如此,在PowerShell中,您可以做到:

  1. 从文本文件加载时
代码语言:javascript
运行
复制
$result = Get-Content -Path 'X:\theInputFile.txt' | Where-Object { $_ -match '^\w+\s*=\s*\S+' }
  1. 解析多行字符串时
代码语言:javascript
运行
复制
$string = @'
variable1 = value1
variable2 = value2
variable3 =
variable4 = value4
variable5 = 
'@ 

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

变量$result将是一个字符串数组:

代码语言:javascript
运行
复制
variable1 = value1
variable2 = value2
variable4 = value4

可以使用以下方法将其保存回(新)文件

代码语言:javascript
运行
复制
$result | Set-Content -Path 'X:\theNewInputFile.txt'

Regex详细信息:

代码语言:javascript
运行
复制
^          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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69963055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档