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

标签 linux awk sed

我的输入文件 (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

第四种解决方案:使用 awkmatch 函数。

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/

相关文章:

awk - 使用 awk 的 header 版本

shell - 精确匹配,前导和尾随特殊字符(包括空格)除外

linux - 会不会是/etc/passwd在后台添加/删除了用户?

linux - 手动运行 Shell 脚本发送新文件通知邮件

linux - 匹配多列并打印二进制结果

linux - 使用 Sed 交换两个字符串

linux - 在 Bash/sed 中分隔字符

linux - 需要在 linux 中使用 sed 编辑文件中特定部分的一行

linux - 增加 FD 限制然后 SSH 不起作用?

linux - if 文件名(只是名称)比较 shell 脚本