提取文件名

用户名

因此,我是SED和Unix的新手,我想替换以下文件:

1500:../someFile.C:111 error
1869:../anotherFile.C:222 error
1869:../anotherFile2.Cxx:333 error
//thousands of more lines with same structure

随着Followig文件

someFile.c
anotherFile.c
anotherFile2.Cxx

基本上,我只想从每一行中提取文件名。

到目前为止,我已经阅读了sed的文档和此处的第二个答案我最好的尝试是使用正则表达式,如下所示:

sed "s/.\*\/.:.*//g" myFile.txt
ghoti

有很多方法可以做到这一点。

当然,您可以使用sed

sed 's/^[^:]*://;s/:.*//;s#\.\./##' input.txt
sed 's%.*:\.\./\([^:]*\):.*%\1%' input.txt

或者,您可以grep -o在管道中使用一系列实例:

grep -o ':[^:]*:' input.txt | grep -o '[^:]\{1,\}' | grep -o '/.*' | grep -o '[^/]\{1,\}'

您甚至可以使用awk

awk -F: '{sub(/\.\.\//,"",$2); print $2}' input.txt

但是最简单的方法可能是使用cut

cut -d: -f2 input.txt | cut -d/ -f2

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章