如何对文件进行子集化 - 选择多个行或列
我希望获得您关于如何对大文件(数百万行或数百万行)进行子集化的建议/帮助。
例如,
(1) 我有一个大文件(数百万行,制表符分隔)。我想要此文件的子集,其中仅包含 10000 到 100000 行。
(2) 我有一个大文件(数百万列,制表符分隔)。我想要这个文件的一个子集,其中只有从 10000 到 100000 的列。
我知道有诸如 head、tail、cut、split 以及 awk 或 sed 之类的工具。我可以用它们来做简单的子集设置。但是,我不知道如何做这项工作。
您能给什么建议吗?提前致谢。
I would like to have your advice/help on how to subset a big file (millions of rows or lines).
For example,
(1)
I have big file (millions of rows, tab-delimited). I want to a subset of this file with only rows from 10000 to 100000.
(2)
I have big file (millions of columns, tab-delimited). I want to a subset of this file with only columns from 10000 to 100000.
I know there are tools like head, tail, cut, split, and awk or sed. I can use them to do simple subsetting. But, I do not know how to do this job.
Could you please give any advice? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
过滤行很容易,例如使用 AWK:
使用 CUT 过滤列更容易:
正如 Rahul Dravid 提到的,
cat
在这里不是必须的,正如 Zsolt Botykai 所补充的,您可以使用以下方法提高性能:Filtering rows is easy, for example with AWK:
Filtering columns is easier with CUT:
As Rahul Dravid mentioned,
cat
is not a must here, and as Zsolt Botykai added you can improve performance using:一些不同的解决方案:
对于行范围:
在
sed
中:对于
awk
中的列范围:Some different solutions:
For row ranges:
In
sed
:For column ranges in
awk
:对于第一个问题,从一个大文件中选择一组行,从尾部到头部进行管道传输非常简单。您需要从第 10000 行开始的大型文件中的 90000 行。tail 抓取从第 10000 行开始的大型文件的后端,然后 head 砍掉除前 90000 行之外的所有行。
For the first problem, selecting a set of rows from a large file, piping tail to head is very simple. You want 90000 rows from largefile starting at row 10000. tail grabs the back end of largefile starting at row 10000 and then head chops off all but the first 90000 rows.
被 sed 解决方案击败,所以我将发布一个
perl
dito 来代替。打印选定的行。
要打印选择性列,请使用
-F
与-a
结合使用,以选择分割行的分隔符。要进行测试,请使用
seq
和paste
生成一些列 让我们打印除第一列和最后一列之外的所有内容
在上面的
join
语句中,有是一个选项卡,您可以通过执行 ctrl-v 选项卡来获取它。Was beaten to it for the sed solution, so I'll post a
perl
dito instead.To print selected lines.
To print selective columns, use
-F
is used in conjunction with-a
, to choose the delimiter on which to split lines.To test, use
seq
andpaste
to get generate some columnsLets's print everything except the first and the last column
In the
join
statement above, there is a tab, you get it by doing a ctrl-v tab.