在Linux中将包含单词的行从一个文件复制到另一个文件 [英] copy lines containing word from one file to another file in linux

查看:139
本文介绍了在Linux中将包含单词的行从一个文件复制到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将包含某些单词的行从file1复制到file2.

I want to copy lines containing certain words from file1 to file2.

假设file1:

ram 100 ct 50
gopal 200 bc 40
ravi 50 ct 40
krishna 200 ct 100

file2应该仅包含包含"ct"的行,如下所示:

file2 should have only the lines containing "ct", which would look like this:

ram 100 ct 50
ravi 50 ct 40
krishna 200 ct 100

哪个是实现此目标的最佳方法?我有一个200mb的文件. 我使用了grep,但是运行grep -n ct file1却没有任何结果.

Which is the best way to achieve this? I had a file of 200mb. I used grep but I didn't get any result running grep -n ct file1.

推荐答案

awk应该做的

awk '/ct/' file1 > file2

如果位置很重要

awk '$3=="ct"' file1 > file2
awk '$3~/ct/' file1 > file2

如果ct是字段#3中的某些内容的一部分,则

最新版本没问题

last version is ok if ct is part of some in field #3

grep

grep ct file1 > file2

不需要

-n,因为它会打印行号

-n is not needed, since it prints line number

sed

sed -n '/ct/p' file1 > file2

这篇关于在Linux中将包含单词的行从一个文件复制到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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