将CSV文件的前x行复制到新的CSV文件中,可以通过Windows命令行完成。

huangapple go评论57阅读模式
英文:

How would one copy only the first x lines of a *csv* file into a new *csv* file via the cmd in windows

问题

head -N file.csv > newfile.csv 这是适用于Mac OS终端的解决方案,请问是否需要Windows cmd上的相同操作?

英文:

How would one copy only the first x number lines of a csv file into a new csv file via the cmd on Windows?

head -N file.csv > newfile.csv
this is the solution for Mac OS terminal, can someone please tell the same for windows cmd?

答案1

得分: 1

If you REALLY want to do it via batch there are several methods for doing it here https://serverfault.com/questions/490841/how-to-display-the-first-n-lines-of-a-command-output-in-windows-the-equivalent

Or the way I used to do it back in the days before Powershell was via the GNU port of the *nix Head command which can be downloaded from https://gnuwin32.sourceforge.net/packages/coreutils.htm

with which you'd extract the first (say 10) lines of text using

Head -n 10 file.csv > newfile.csv

But the easier native way IMHO is to just doing it using Powershell. In which case

Import-Csv -Path c:\folder\file.csv -Delimiter "," | 
    Select -First 10 |
    Export-Csv -Path C:\folder\newfile.csv -NoTypeInformation

would do what you need.

英文:

If you REALLY want to do it via batch there are several methods for doing it here https://serverfault.com/questions/490841/how-to-display-the-first-n-lines-of-a-command-output-in-windows-the-equivalent

Or the way I used to do it back in the days before Powershell was via the GNU port of the *nix Head command which can be downloaded from https://gnuwin32.sourceforge.net/packages/coreutils.htm

with which you'd extract the first (say 10) lines of text using

Head -n 10 file.csv > newfile.csv

But the easier native way IMHO is to just doing it using Powershell. In which case

Import-Csv -Path c:\folder\file.csv -Delimiter "," | 
    Select -First 10 |
    Export-Csv -Path C:\folder\newfile.csv -NoTypeInformation

would do what you need.

huangapple
  • 本文由 发表于 2023年5月21日 13:38:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定