如果这是一个愚蠢的问题,请原谅我,我是PowerShell的新手。因此,我有一个供应商将平面文件发送到我的数据收集系统,该文件由零件的序列号命名。我有多台发送文件的计算机,这些文件都在中央计算机上进行数据集中。我的经理设置了Powershell脚本,以映射共享驱动器上的驱动器并将这些文件移动到共享驱动器上的文件夹中。我想将这些Powershell脚本中的文件分离到每台计算机的文件夹中。平面文件如下所示:
Premachine ID:
Main Machine name ID: ,ABCD1234Z
More stuff: ,Stuff
Date: ,02/02/22
Time: ,5:55:55 am
Filename: ,c:\exportfolder\SN_GOBBLY_GOOK_2_02_2022.CSV
Events:
Alarms:
Header1, Header2, Header3, Header4, Header5, Etc
1,2,3,4,5,6
1,2,3,4,5,6
我只想根据主要ID ABCD1234Z将文件移动到文件夹中。
我想使用类似的东西
$press = Import-Csv Share:\folder\SN_GOBBLY_GOOK_2_02_2022.CSV -Delimiter "," | select -ExpandProperty Main Machine name ID:
和
$press = Import-Csv Share:\folder\SN_GOBBLY_GOOK_2_02_2022.CSV -Delimiter ":" | select -ExpandProperty Main Machine name ID
如果我可以将数据放入变量,则可以从那里获取。
答案 0 :(得分:0)
如果所有这些文件的格式都一样,那么您可以在几行中执行以下操作:
//declare variables the prompts the user to state their birthdate in
//month, day and year
var birthYear = Number(prompt('What year were you born in?'));
var birthMonth = Number(prompt('What month were you born in? In numerals please!'));
var birthDay = Number(prompt('What day of the month were you born on? In numerals please!'));
//declare variables to get current date
var now = new Date();
var currentYear = now.getFullYear();
var currentMonth = now.getMonth() + 1; //so now January = 1
var currentDay = now.getDate();
//declare variable for age will turn this year
var age = currentYear - birthYear;
//declare variable for text output
var text = "";
//create if/else loop for three different scenarios
if (birthMonth < currentMonth) {
text += "You have turned " + age + " years old already this year.";
} else if (birthMonth > currentMonth) {
text += "You will be turning " + age + " years old later this year.";
} else if (birthMonth === currentMonth && birthDay === currentDay) {
text += "Today is your Birthday! Happy Birthday!";
} else (birthMonth === currentMonth && birthDay !== currentDay) {
text += "Your birthday is this month! Lucky you!";
}
$lines = (Get-Content ./test.csv) -Split "`n"
$line = $lines | Select-String -Pattern "Main Machine name ID:"
$partNumber = $line.ToString().Split(",")[1]
是ABCD1234Z
您当然可以将其减少到2行,但可读性和全部。
答案 1 :(得分:0)
如果这始终是第2行(数组中的索引1),则可以在一行中完成。
# mock file data
$data = @'
Premachine ID:
Main Machine name ID: ,ABCD1234Z
More stuff: ,Stuff
Date: ,02/02/22
Time: ,5:55:55 am
Filename: ,c:\exportfolder\SN_GOBBLY_GOOK_2_02_2022.CSV
Events:
Alarms:
Header1, Header2, Header3, Header4, Header5, Etc
1,2,3,4,5,6
1,2,3,4,5,6
'@
# testing, save to a temp file
$data | Out-File -FilePath (Join-Path $env:TEMP 'so_test.txt')
#
# as a one-liner
#
$name = ((Get-Content (Join-Path $env:TEMP 'so_test.txt'))[1] -split ',')[1].Trim()
Write-Output "Found name: '$name'."
涉及的步骤。
有你的名字。
答案 2 :(得分:0)
此方法使用正则表达式获取您要使用的ID:
# read the data from the flat file in one single string using the '-Raw' switch
$data = Get-Content -Path 'PATH TO THE FLAT FILE' -Raw
$machineID = ([regex]'Main Machine.*ID:\s*,?(\w+)').Match($data).Groups[1].Value
此后,$machineID
像在此示例ABCD1234Z
中一样保留ID,或者如果平面文件中不包含“ Main Machine ...”行,则为空字符串。
正则表达式详细信息
Main Machine Match the characters “Main Machine” literally
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
ID: Match the characters “ID:” 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)
, Match the character “,” literally
( Match the regular expression below and capture its match into backreference number 1
\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)
)