简体   繁体   English

从文本文件中提取特定行

[英]extracting specific lines from a text file

I have a data in my .txt file as below, I want to extract the line that have value as 12 and copy it into new .txt file. 我的.txt文件中有一个数据,如下所示,我想提取值为12的行并将其复制到新的.txt文件中。 I tried with sed but could get the result, any help would be appreciated . 我尝试用sed但可以得到结果,任何帮助将不胜感激。 Thanks 谢谢

"944760 1939"   10
"944760 1940"   12
"946120 1940"   2
"946370 1939"   10
"946370 1940"   12
"946460 1940"   6
"946530 1939"   10

Why don't just search forum, before posting here, so many posts repeated 为什么不在搜索论坛之前发布这么多帖子重复

awk '$3 == 12' infile > outfile

which is same as 与...相同

awk '$3 == 12 { print }' infile > outfile 

Explanation 说明

  • $3 == 12 if 3rd column is equal to 12, print such record/row $3 == 12如果第3列等于12,则打印此类记录/行

Following simple awk may help you on same: 以下简单的awk可以帮助你:

awk '$NF==12{print $0 > "new_file"}'   Input_file

Explanation: 说明:

$NF==12 : Checking condition here if last field's value is 12 means condition is TRUE then perform further/following statements. $NF==12 :如果最后一个字段的值为12则在此检查条件意味着条件为TRUE,然后执行进一步/后续语句。

{print $0 > "new_file"} : Printing current line($0) value to a output file named {print $0 > "new_file"} :将当前行($ 0)值打印到名为的输出文件

new_file (you could change its name as per your wish too). new_file (您也可以根据自己的意愿更改其名称)。

Input_file : Mentioning Input_file name here. Input_file :在这里提到Input_file名称。

Solution 3rd: As per karafka sir's comment adding this one too now. 解决方案第3名:根据karafka先生的评论,现在也添加了这个。

awk'$NF==12' Input_file > "new_file"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM