我的输入文件 (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
我需要在“found in file”这个词之后提取所有数据才能得到这个 输出:
/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}'
它有效,但并非在所有情况下都有效,即如果有很多空格,则不会返回最新的单词。
是否有其他更好的方法来获得相同的输出?
最佳答案
第一个解决方案:这应该可以通过 awk
轻松完成。
awk '{sub(/.*found in file/,"")} 1' Input_file
第二种解决方案: 或者更准确地说,将其本身作为字段分隔符:)
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
第 5 种解决方案:使用 grep
的 \K
选项(已使用提供的示例进行测试)。
grep -Po "found in file \K.*" Input_file
关于linux - 使用 linux 命令从文件中提取最后一部分数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58602952/