【发布时间】:2019-06-25 17:21:10
【问题描述】:
我有一个以这种格式开头的文件
INFO|NOT-CLONED|/folder/another-folder/another-folder|last-folder-name|
我需要的是读取文件并得到这个输出:
INFO|NOT-CLONED|last-folder-name
到目前为止我有这个:
cat clone_them.log | grep 'INFO|NOT-CLONED' | sed -E 's/INFO\|NOT-CLONED\|(.*)/g'
但没有按预期工作
注意:最后的“another-folder”和“last-folder-name 是一样的”
【问题讨论】:
-
一般情况下不需要
grep pattern | sed ...。您可以使用sed进行过滤。在这种情况下:sed -n '/INFO|NOT-CLONED/s/...//p。请注意,我已将您的替换替换为...,因为sed是错误的工具。我只是指出grep | sed是一种反模式。 -
也可以使用
cut,cat clone_them.log | cut -d'|' -f3