我有一个来自API的文本文件,我只需要删除特定列(第5列)中的空白
示例:
"Number"|"Message"|"Reference"|"SendFrom"|"CampaignName"
"4478xxxxxxxx"|"Test message from test system"|"104"|"dunno"|"Campaign Name Here 2"
"4479xxxxxxxx"|"Test message from test system"|"105"|"dunno"|"Campaign Name Here 2"
我需要输出如下:
"Number"|"Message"|"Reference"|"SendFrom"|"CampaignName"
"4478xxxxxxxx"|"Test message from test system"|"104"|"dunno"|"CampaignNameHere2"
"4479xxxxxxxx"|"Test message from test system"|"105"|"dunno"|"CampaignNameHere2"
因此只有最后一列的空白已删除,而不是文件的其余部分。
我能够隔离最后一列并删除空格:
$columnToGet4 = 4
$columns4 = gc $Report |
%{ $_.Split("|",[StringSplitOptions]"RemoveEmptyEntries")[$columnToGet4] }
$columns4 = $columns4 -replace '\s',''
但是尝试将数据重新编织在一起是行不通的。
有关如何实现此目标的任何建议?
谢谢
答案 0 :(得分:1)
您可以为此使用PowerShell的CSV处理:
# Import the file as if it was a CSV separated by the pipe,
# and process each row
Import-Csv -LiteralPath 'c:\temp\input.txt' -Delimiter '|' |
ForEach-Object {
# Replace spaces in the campaignName column, by name
$_.CampaignName = $_.CampaignName -replace '\s'
# and output the changed row item, for export to file
$_
} | Export-Csv -LiteralPath 'c:\temp\output.txt' -Delimiter '|' -NoTypeInformation
或者纯文本处理和正则表达式替换:
Get-Content -LiteralPath 'c:\temp\input.txt' | ForEach-Object {
$_ -replace '\s(?=[^|]+$)'
} | Set-Content -LiteralPath 'c:\temp\output.txt' -Encoding ASCII
regex会选择“此后行中不再有管道的空间”(可能不是有效的假设)。
或者您可以使用纯文本处理,选择最后一个竖线字符在哪里:
Get-Content -LiteralPath 'c:\temp\input.txt' | foreach-object {
$afterLastPipe = $_.lastindexof('|')+1
$_.Substring(0, $afterLastPipe) + $_.Substring($afterLastPipe).Replace(' ', '')
} | Set-Content...
同样,可能没有一个有效的假设,那就是不再有管道,尤其是如果引号内有一个管道。
答案 1 :(得分:1)
假设管道符号是分隔符,而不仅仅是显示标记,那么就可以完成工作... [ grin ]
# fake reading in a CSV file
# in real life, use Import-CSV -Delimiter '|'
$InStuff = @'
"Number"|"Message"|"Reference"|"SendFrom"|"CampaignName"
"4478xxxxxxxx"|"Test message from test system"|"104"|"dunno"|"Campaign Name Here 2"
"4479xxxxxxxx"|"Test message from test system"|"105"|"dunno"|"Campaign Name Here 2"
'@ | ConvertFrom-Csv -Delimiter '|'
foreach ($IS_Item in $InStuff)
{
$IS_Item.CampaignName = $IS_Item.CampaignName.Replace(' ', '')
}
# on screen
$InStuff
# send to CSV file
$ECSV_Params = @{
LiteralPath = "$env:TEMP\Pablo_Beezo_-_DeSpacedCampaignNameVersion.csv"
Delimiter = '|'
NoTypeInformation = $True
}
$InStuff |
Export-Csv @ECSV_Params
屏幕上...
Number : 4478xxxxxxxx
Message : Test message from test system
Reference : 104
SendFrom : dunno
CampaignName : CampaignNameHere2
Number : 4479xxxxxxxx
Message : Test message from test system
Reference : 105
SendFrom : dunno
CampaignName : CampaignNameHere2
CSV文件内容...
"Number"|"Message"|"Reference"|"SendFrom"|"CampaignName"
"4478xxxxxxxx"|"Test message from test system"|"104"|"dunno"|"CampaignNameHere2"
"4479xxxxxxxx"|"Test message from test system"|"105"|"dunno"|"CampaignNameHere2"
答案 2 :(得分:0)
也许这使得正在发生的事情更加清楚:
$Report = Get-Content ./Data.txt
$Result = foreach ($R in $Report) {
# Split the original text string into pieces, an array
$OriginalArray = $R.Split('|')
# Only execute when there are 4 elements
if ($OriginalArray.Count -ge 5) {
# Remove the spaces of the 4th element in the array
$UpdatedValue = $OriginalArray[4] -replace '\s'
# Update the original value
$OriginalArray[4] = $UpdatedValue
}
# Join the array back together into one string
$OriginalArray -join '|'
}
$Result
# Export the data to a file
$Result | Out-File .File.txt -Encoding UTF8
答案 3 :(得分:0)
这是单线的:
Import-Csv -Path $source -Delimiter '|' -Encoding UTF8 | % { $_.CampaignName = $_.CampaignName.Replace(' ',''); $_ } | Export-Csv $dest -NoTypeInformation -Delimiter '|'