[英]copy lines containing word from one file to another file in linux
我想復制包含從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領域的一部分,那么最后一個版本是可以的
與grep
相同
grep ct file1 > file2
-n
不需要,因為它打印行號
與sed
相同
sed -n '/ct/p' file1 > file2
egrep -n '\bct\b' file1 > file2
順便說一下,grep -n選擇行和行號。 如果這不是你的意圖,那么grep ct file1 > file2
就足夠了。
awk '$3=="ct"' file1 > file2
這僅過濾第三列必須與字符串ct
完全匹配的行。
我知道我遲到了,但只是想補充說你可以在vim中做到這一點。 創建該文件的副本,然后運行
:g!/pattern/d
(刪除所有沒有模式的行)
所以,
:g!/^\w+ \d+ ct \d+/ \\ view the lines
:g!/^\w+ \d+ ct \d+/d \\ delete lines
可在此處找到更多命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.