使用powershell在csv中搜索并组合两个字段



我想在当前目录中的每个文件中搜索一个字符串,组合结果,并输出一个文本文件。

我要找的字符串是";开始l〃;。这将返回两个结果;起始纬度";以及";起始经度";。我想将这些数据合并为每个文件的一行。

这里有一个例子:

select-string -path .CC2019012_20220601_125156.txt -Pattern 'Start l'

哪个收益率:

CC2019012_20220601_125156.txt:10:%起始纬度,39.0285453

CC2019012_20220601_125156.txt:11:%起始经度,-74.4891558

我想创建的是:

CC2019012_20220601_125156,39.0285453-74.4891558

以下是我在Linux下的操作方法:

for file in *.txt
do
lat=$(grep -i "start l" $file | dos2unix | sed -e 's/^..*,//' | sed -e 'N;s/n/,/')
print $file,$lat
done >| locations.sam

如何在powershell中获得相同的结果?

埃塔:我发现了一些有效的东西。。。也许有人可以改进我的想法?

foreach ($file in gci *.txt)
{
$base = (Get-Item "$file" ).Basename
$var =  select-string -path $file -Pattern 'Start l' | %{$_ -replace "..*,",""}
$join = $var -join ","
echo "$base,$join" | out-file -Append .locations.sam
}

示例CSV文件:

% Device,CC2206006
% File name,CC2206006_20220712_103027
% Cast time (UTC),2022-07-12 10:30:27
% Cast time (local),2022-07-12 10:30:27
% Sample type,Cast
% Cast data,Down
% Location source,GPS
% Default latitude,32
% Default altitude,0
% Start latitude,34.0449595
% Start longitude,-73.4006081
% Start altitude,-30.834999084472656
% Start GPS horizontal error(Meter),32.823001861572266
% Start GPS vertical error(Meter),641.02001953125
% Start GPS number of satellites,3
% End latitude,34.0461822
% End longitude,-73.4019048
% End altitude,-33.549999237060547
% End GPS horizontal error(Meter),214.10699462890625
% End GPS vertical error(Meter),463.32901000976562
% End GPS number of satellites,3
% Cast duration (Seconds),84.2
% Samples per second,5
% Electronics calibration date,0001-01-01
% Conductivity calibration date,2022-02-10
% Temperature calibration date,2022-02-09
% Pressure calibration date,2022-02-08
%
Pressure (Decibar),Depth (Meter),Temperature (Celsius),Conductivity (MicroSiemens per Centimeter),Specific conductance (MicroSiemens per Centimeter),Salinity (Practical Salinity Scale),Sound velocity (Meters per Second),Density (Kilograms per Cubic Meter)
0.15,0.14997177615318641,22.042546929715112,43719.05456967745,46467.5675777045,30.112056249182295,1521.6050401503587,1020.4990404995208
21.75,21.697195002693061,11.70683765411377,37383.1953125,50921.296550740095,32.706304507268229,1493.4325554024592,1024.9609682732307
22.119968011090421,22.065484920621859,11.69420678760779,37442.975677761278,51020.282158553651,32.775522366290218,1493.4778352736607,1025.0187132969579

您也可以使用非常快速的switch来获得您想要的值,使用regex,将这些值收集在一个变量中,完成后将其全部写入locations.sam文件,如下所示:

# go through the text files, filter out the wanted values 
# and collect formatted lines in variable $data
$data = foreach ($file in (Get-ChildItem -Filter '*.txt')) {
$values = switch -Regex -File $file.FullName {
'% Start l(at|ong)itude' { ($_ -split ',')[-1]}
}
# output a formatted line
'{0},{1}' -f $file.Basename, ($values -join ',')
}
# now append the data to the output file
# using PassThru, it will also display in the console
$data | Add-Content -Path .locations.sam -PassThru

相关内容

  • 没有找到相关文章

最新更新