问题
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
I'm using this
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 ?
回答1:
1st Solution: This should be easily done by 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 's/.*found in file\(.*\)/\1/' Input_file
4th solution: Using match function of 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).
grep -Po "found in file \K.*" Input_file
来源:https://stackoverflow.com/questions/58602952/extract-last-section-of-data-from-file-using-linux-command