[英]Powershell - .csv split strings
I am trying to use powershell for extracting email addresses from a.csv file.我正在尝试使用 powershell 从 a.csv 文件中提取 email 地址。 Each row in the.csv may have none or more emails separated by ",". .csv 中的每一行可能没有或更多的电子邮件由“,”分隔。
fe铁
Email Email
info@domain.com, email@domain.com, person@contonso.com info@domain.com,电子邮件@domain.com,person@contonso.com
something@domain.com something@domain.com
My goal is to write it that way so I can get the "info@ " from the row if it is present + 1 extra email from the row if it is present.我的目标是这样写,这样我就可以从行中获取“info@”(如果存在)+ 1 个额外的 email(如果存在)。 If there is no "info@ " get at least 1 or 2 emails from that row.如果没有“info@ ”,则从该行中获取至少 1 或 2 封电子邮件。
Here is the fracture of the code, where I am manually able to say on what position is what email, but I am not able to get this part to work in the for cycle which I could use to enumerate the number of occurences as it appears I cannot convert it to int at all.这是代码的断裂,我可以手动说出 position 是什么 email,但是我无法让这部分在 for 循环中工作,我可以用它来枚举出现的次数我根本无法将其转换为 int。
$Occurrences = $email.Split(",").GetUpperBound(0);
[int]$Occurrences
$data = Import-Csv -path $path
foreach($contact in $data)
{
$email = $contact.Email
if($email.Contains("info"))
{
$emailSplit = $contact.Email.Split(",")
$Occurrences = $email.Split(",").GetUpperBound(0);
[int]$Occurrences
$name = $domainSplit[0]
for([int]$i = 0;-lt $Occurrences.ToInt32(); $i++)
{
}
}
}
Any help is appreciated.任何帮助表示赞赏。
This is not a valid CSV Format.这不是有效的 CSV 格式。 Cant you export the data via JSON from the datasource?您不能通过 JSON 从数据源导出数据吗?
You need to split the single lines and then do your operations您需要拆分单行,然后进行操作
$data = Get-Content -path $path
for($i=1; $i -lt $data.Length; $i++)
{
$emailSplit = [array]$data[$i].Split(",")
for($j = 0; $j -lt $emailSplit.Length; $j++) {
<#do your operation here...
loop once through the elements, check for info@, and then assign them accoringly...
#>
}
}
V2: V2:
$data = Get-Content -path $path
for($i=1; $i -lt $data.Length; $i++)
{
$emailSplit = [array]$data[$i].Split(",")
Write-Host ('Results for line: ' + $i)
$infoFound = $false
for($j = 0; $j -lt $emailSplit.Length; $j++) {
if($emailSplit[$j] -match 'Info@*') {
$infoFound = $true
$infoPos = $j
}
}
[array]$results = $emailSplit[0]
$results += $emailSplit[-1]
if($infoFound) {
if($infoPos = 0) {$results[1] = $emailSplit[$infoPos]}
else {$results[0] = $emailSplit[$infoPos]}
}
Write-Host ('Element1: ' + $results[0] + ' Element2: ' + $results[1])
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.