在文件中选择大量(特定)行 [英] selecting a large number of (specific) rows in file

查看:54
本文介绍了在文件中选择大量(特定)行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从大文件中选择大量特定行.

I want to select a large number of specific rows from large files.

使用 perl,我正在创建这种样式的命令(此处打印第 2 行和第 4 行):

Using perl, I am creating a command of this style (here printing 2nd and 4th rows):

sed -n -e 2p -e 4p $file

并使用 system() 命令启动它.

And launching it using a system() command.

这很好用,除非要从文件中选择的行数变得非常大.它目前适用于 ~10,000 行,但不适用于我想选择 ~17,000 行的另一个文件.可以传递给 sed 的参数数量是否有限制?是否有替代的 UNIX 工具可供使用?感谢您的帮助

This works fine, except when the number of rows to select from a file becomes quite larger. It currently works with ~10,000 rows, but not with another file where I want to select ~17,000 rows. Is there a limit in the number of arguments that can be passed to sed? Would there be an alternative UNIX tool to use? Thanks for your help

推荐答案

一个 sed 命令:

使用;分隔符代替几个-e 2p

sed -n -e '2p;4p;12p' file

如果 bash 命令行太长

if too long for bash command line

2p;4p;12p中创建一个结构相同的临时文件并使用-f选项

create a temporary file with the same structure inside 2p;4p;12p and use -f option

sed -n -f TemporaryFile file

这篇关于在文件中选择大量(特定)行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆