使用linux命令从文件中提取数据的最后一部分 [英] extract last section of data from file using linux command
本文介绍了使用linux命令从文件中提取数据的最后一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的输入文件(myfile)为:
I have the input file (myfile) as:
>> Vi 'x' found in file /data/152.916612:2,/proforma invoice.doc
>> Vi 'x' found in file /data/152.48152834/Bank T.T Copy 12 d3d.doc
>> Vi 'x' found in file /data/155071755/Bank T.T Copy.doc
>> Vi 'x' found in file /data/1521/Quotation Request.doc
>> Vi 'x' found in file /data/15.462/Quotation Request 2ds.doc
>> Vi 'y' found in file /data/15.22649962_test4/Quotation Request 33 zz (.doc
>> Vi 'x' found in file /data/15.226462_test6/Quotation Request.doc
我需要提取在文件中找到"一词之后的所有数据,以具有此功能输出:
and I need to extract all data after the words "found in file " to have this output:
/data/152.18224487:2,S/proforma invoice.doc
/data/152.916612:2,/proforma invoice.doc
/data/152.48152834/Bank T.T Copy 12 d3d.doc
/data/155071755/Bank T.T Copy.doc
/data/1521/Quotation Request.doc
/data/15.462/Quotation Request 2ds.doc
/data/15.22649962_test4/Quotation Request 33 zz (.doc
/data/15.226462_test6/Quotation Request.doc
我正在使用这个
grep "" myfile | awk '{print $7" "$8" "$9" "$10}'
它可以工作,但并非在所有情况下都有效,即,如果有很多空格,则不会返回最新单词.
it works but not in all situations , i.e. if there are a lot of spaces, latest words are not returned.
还有其他更好的方法来获得相同的输出吗?
Are there other better ways to have the same output ?
推荐答案
第一个解决方案: 此操作应通过 awk
轻松完成.
awk '{sub(/.*found in file/,"")} 1' Input_file
第二个解决方案: 或者更精确地说,将其作为字段分隔符:)
2nd solution: OR more precisely with making it as a field separator itself :)
awk -F"found in file" '{print $2}' Input_file
第三种解决方案: 使用 sed
:
sed 's/.*found in file\(.*\)/\1/' Input_file
第四种解决方案: 使用 awk
的 match
功能.
awk 'match($0,/found in file/){print substr($0,RSTART+RLENGTH+1)}' Input_file
第五种解决方案: :使用 grep
的 \ K
选项(已通过提供的示例进行了测试).>
5th solution: Using grep
's \K
option(tested with provided samples).
grep -Po "found in file \K.*" Input_file
这篇关于使用linux命令从文件中提取数据的最后一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文