字符串修剪和拆分 [英] String trim and split

查看:29
本文介绍了字符串修剪和拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我阅读的文本文件,我需要从中获取值.

示例文本文件:

<前>[站点01]DBServer=本地主机数据库名称=数据库01用户名=管理员密码=qwerty[站点02]数据库服务器=192.168.0.10数据库名称=数据库02用户名=管理员密码=qwerty

目前我的代码读取文件并将每个文件作为找到的每一行 DBServer= 的数组条目,并且此文本文件可以有多个站点:

$NumOfSites = Get-Content $Sites |Select-String -Pattern "DBServer=" -Context 0,3$i = 0$NumOfSites |ForEach-Object {$svr = $NumOfSites[$i] -isplit "\n" |% { ($_ -isplit 'DBServer=').Trim()[1] }$db = $NumOfSites[$i] -isplit "\n" |% { ($_ -isplit 'DBName='.Trim())[1] }$uid = $NumOfSites[$i] -isplit "\n" |% { ($_ -isplit '用户名='.Trim())[1] }$pswd = $NumOfSites[$i] -isplit "\n" |% { ($_ -isplit '密码='.Trim())[1] }$i = $i+1}

我无法在没有一些额外空格或作为字符串变量的情况下正确拆分每个属性.我只需要从我拥有的文件示例的格式中提取信息以作为变量放入 SQL 连接行.

解决方案

除了记录头(即[Site 01]),其余都可以通过ConvertFrom-StringData处理代码>就好了.我们可以或多或少地将记录转换为直接在标题行上拆分的对象.ConvertFrom-StringData 将多行字符串转换为哈希表,您可以将其转换为 [PSCustomObject] 和 viola,您拥有易于使用的对象.

$NumOfSites = Get-Content $Sites -raw$SiteObjects = $NumOfSites -split '\[.+?\]'|%{[PSCustomObject](ConvertFrom-StringData -StringData $_)}

然后您可以操作 $SiteObjects,但您认为合适(如果需要,输出到 CSV,或使用 Select-Object 过滤任何属性).或者,如果您想建立连接,您可以循环遍历它,根据需要建立连接...

ForEach($SiteObjects 中的 $Connection){$ConStr = "服务器 = {0}; 数据库 = {1}; 集成安全 = False; 用户 ID = {2}; 密码 = {3};"-f $Connection.DBServer.Trim(), $Connection.DBName.Trim(), $Connection.Username.Trim(), $Connection.Password.Trim()<用 SQL 做事>}

更新我的答案,因为示例文本已更改为添加

.我们只需要删除它们,因为 OP 会收到有关空值方法的错误,我们也将过滤空值.

$NumOfSites = Get-Content $Sites -raw$SiteObjects = $NumOfSites -replace '<.*?>'-split '\[.+?\]' |?{$_} |%{[PSCustomObject](ConvertFrom-StringData -StringData $_)}ForEach($SiteObjects 中的 $Connection){$svr = $Connection.DBServer.Trim()$db = $Connection.DBName.Trim()$uid = $Connection.Username.Trim()$pwd = $Connection.Password.Trim()}

I have a text file that I read and I need to get the values from.

Example text file:

[Site 01]
DBServer=LocalHost
DBName=Database01
Username=admin
Password=qwerty

[Site 02]
DBServer=192.168.0.10
DBName=Database02
Username=admin
Password=qwerty

Currently my code reads through the file and places each each as an array entry for each line DBServer= that is found and this text file can have many sites:

$NumOfSites = Get-Content $Sites |
              Select-String -Pattern "DBServer=" -Context 0,3
$i = 0
$NumOfSites | ForEach-Object {
    $svr  = $NumOfSites[$i] -isplit "\n" |
            % { ($_ -isplit 'DBServer=').Trim()[1] }
    $db   = $NumOfSites[$i] -isplit "\n" |
            % { ($_ -isplit 'DBName='.Trim())[1] }
    $uid  = $NumOfSites[$i] -isplit "\n" |
            % { ($_ -isplit 'Username='.Trim())[1] }
    $pswd = $NumOfSites[$i] -isplit "\n" |
            % { ($_ -isplit 'Password='.Trim())[1] }
    $i = $i+1
}

I can't get each attribute to split out properly without some extra spaces or something nicely as a string variable. I just need to extract the info to put into an SQL connection line as variables from the format of the file example I have.

解决方案

Other than the record headers (i.e. [Site 01]) the rest can be handled by ConvertFrom-StringData just fine. We can just convert the records to objects directly splitting on the header row more or less. ConvertFrom-StringData turns a multi-line string into a hashtable, and you can just cast that as a [PSCustomObject] and viola, you have objects that are easy to use.

$NumOfSites = Get-Content $Sites -raw
$SiteObjects = $NumOfSites -split '\[.+?\]'|%{[PSCustomObject](ConvertFrom-StringData -StringData $_)}

Then you can manipulate $SiteObjects however you see fit (output to CSV if you want, or filter on any property using Select-Object). Or, if you're looking to make connections you can loop through it building your connections as needed...

ForEach($Connection in $SiteObjects){
    $ConStr = "Server = {0}; Database = {1}; Integrated Security = False; User ID = {2}; Password = {3};" -f $Connection.DBServer.Trim(), $Connection.DBName.Trim(), $Connection.Username.Trim(), $Connection.Password.Trim()
    <Do stuff with SQL>
}

Edit: Updating my answer since the sample text was changed to add <pre> and </pre>. We just need to remove those, and since the OP is getting errors about methods on null values we'll filter for null as well.

$NumOfSites = Get-Content $Sites -raw
$SiteObjects = $NumOfSites -replace '<.*?>' -split '\[.+?\]' | ?{$_} |%{[PSCustomObject](ConvertFrom-StringData -StringData $_)}
ForEach($Connection in $SiteObjects){ 
    $svr = $Connection.DBServer.Trim() 
    $db  = $Connection.DBName.Trim() 
    $uid = $Connection.Username.Trim() 
    $pwd = $Connection.Password.Trim()
}

这篇关于字符串修剪和拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆