我有多个 *.csv 文件。我想在 powershell 脚本中将它们连接到单个 CSV 文件中。所有 csv 文件都有相同的标题(第一行),因此当我连接它们时,我只想保留第一个文件中的第一行。
我怎样才能做到这一点?
注:解决方案为这个答案有意使用纯文本处理处理文件,有两个原因:
也就是说,Import-Csv
and Export-Csv
每当您需要时,这肯定是更好的选择阅读和解释数据(而不是仅仅将其复制到其他地方) - 请参阅Sid 的有用回答.
# The single output file.
# Note: Best to save this in a different folder than the input
# folder, in case you need to run multiple times.
$outFile = 'outdir/out.csv'
# Get all input CSV files as an array of file-info objects,
# from the current dir. in this example
$inFiles = @(Get-ChildItem -Filter *.csv)
# Extract the header line (column names) from the first input file
# and write it to the output file.
Get-Content $inFiles[0] -First 1 | Set-Content -Encoding Utf8 $outFile
# Process all input files and append their *data* rows to the
# output file (that is, skip the header row).
# NOTE: If you only wanted to extract a given count $count of data rows
# from each file, add -First ($count+1) to the Get-Content call.
foreach ($file in $inFiles) {
Get-Content $_.FullName | Select-Object -Skip 1 |
Add-Content -Encoding Utf8 $outFile
}
Note the use of -Encoding Utf8
as an example; adjust as needed; by default, Set-Content
will use "ANSI" encoding in Windows PowerShell, and BOM-less UTF-8 in PowerShell Core.
Caveat:通过逐行进行纯文本处理,您依赖于每个文本行代表单个 CSVdata row;这是通常确实如此,但不一定如此。
相反,如果性能至关重要,则通过直接使用 .NET 方法(例如[IO.File]::ReadLines()或者,如果文件足够小,甚至[IO.File]::ReadAllLines().
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)