简体   繁体   English

使用 linux 命令从文件中提取最后一段数据

[英]extract last section of data from file using linux command

I have the input file (myfile) as:我的输入文件(myfile)为:

>> 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:我需要提取“在文件中找到”字样之后的所有数据才能拥有这个 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   

I'm using this我正在使用这个

grep "" myfile  |   awk '{print $7" "$8" "$9" "$10}'

it works but not in all situations, ie if there are a lot of spaces, latest words are not returned.它有效,但并非在所有情况下都有效,即如果有很多空格,则不会返回最新的单词。

Are there other better ways to have the same output?还有其他更好的方法来拥有相同的 output 吗?

1st Solution: This should be easily done by awk .第一个解决方案:这应该可以通过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

3rd solution: With sed :第三种解决方案:使用sed

sed 's/.*found in file\(.*\)/\1/' Input_file

4th solution: Using match function of awk .第四种解决方案:使用match awk 的awk

awk 'match($0,/found in file/){print substr($0,RSTART+RLENGTH+1)}' Input_file

5th solution: Using grep 's \K option(tested with provided samples).第 5 种解决方案:使用grep\K选项(使用提供的样品进行测试)。

grep -Po "found in file \K.*" Input_file

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

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