将CSV文件的前x行读入新的outfile? [英] Read first x lines of csv file into new outfile?
问题描述
如何通过终端仅将csv
文件的前x行复制到新的csv
文件中?
How would one copy only the first x lines of a csv
file into a new csv
file via the terminal?
推荐答案
简介
(您将使用linux终端/控制台)
Brief
(You'll use a linux terminal/console)
使用head -n NUMBEROFLINES file.csv
来获取第一个NUMBEROFLINES
行.像这样使用shell重定向(>
)将其写入另一个文件:
Use head -n NUMBEROFLINES file.csv
to get the first NUMBEROFLINES
of lines. Write it into another file using shell redirection (>
) like this:
head -n NUMBEROFLINES file.csv > mynewfile.csv
请注意,这将完全重新创建mynewfile.csv
,,如果该内容中有任何内容,则现在将其永久删除(-ish).
Note that this will totally recreate mynewfile.csv
, if it had any content before it is now deleted forever(-ish).
如果您碰巧想要相反的内容(最后 x行),请使用tail
.
If you ever happen to want the opposite (last x lines), use tail
.
这两个工具都带有手册页和信息页(man head
或info head
-习惯于man
)和--help
标志(head --help
实际上或多或少显示了手册页).
Both tools come with man and info pages (man head
or info head
- get used to man
, though) and a --help
flag (head --help
actually shows me more or less the man page).
head -n 10 data.csv >> /tmp/first_and_last.csv # Note the ">>"
tail -n 10 data.csv >> /tmp/first_and_last.csv # Note the ">>"
这将打开文件/tmp/first_and_last.csv
,并在/tmp/first_and_last.csv
的结尾"处附加(>>
,>
将重新创建/删除文件!)data.csv
的前10行.
This would open the file /tmp/first_and_last.csv
and attach (>>
, >
would recreate/delete the file!) the first and the last 10 lines of data.csv
at the "end" of /tmp/first_and_last.csv
.
Mac OS: 根据Internet(tm),这些命令在基于Mac的Mac OS中也可用(您必须通过Finder启动终端).
Mac OS: According to the internet (tm) these commands are available in (Unix-based) Mac OS as well (you have to start the Terminal via Finder).
更多口语示例
-n
是--lines=
的缩写,因此您也可以使用:
-n
is short for --lines=
, so you could also use:
tail --lines=10 data.csv >> addtothisfile.txt
head --lines=10 data.csv >> addtothisfile.txt
这篇关于将CSV文件的前x行读入新的outfile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!