我想将包含某些单词的行从file1
复制到file2
。
假设file1
:
ram 100 ct 50 gopal 200 bc 40 ravi 50 ct 40 krishna 200 ct 100
file2
应该只有包含“ct”的行,如下所示:
ram 100 ct 50 ravi 50 ct 40 krishna 200 ct 100
哪一个是达到这个目标的最好方法? 我有一个200MB的文件。 我使用了grep
但是我没有得到任何运行grep -n ct file1
。
这awk
应该做的
awk '/ct/' file1 > file2
如果职位很重要
awk '$3=="ct"' file1 > file2 awk '$3~/ct/' file1 > file2
如果ct
是字段#3中某些ct
的一部分,则最后的版本是可以的
和grep
grep ct file1 > file2
-n
不需要,因为它会打印行号
和sed
sed -n '/ct/p' file1 > file2
awk '$3=="ct"' file1 > file2
这只会过滤第三列必须与字符串ct
完全匹配的行。
egrep -n '\bct\b' file1 > file2
顺便说一句,grep -n和行号一起选择行。 如果这不是你的意图,那么grep ct file1 > file2
就足够了。