【发布时间】:2017-08-10 07:35:34
【问题描述】:
我在带有反引号的 perl 脚本中使用 grep。
grep -r --include=*.txt -e '[a-zA-Z0-9]*\.[a-zA-Z]*$' $dir -n >> test.txt;
我正在尝试过滤掉以文件名结尾的行。
示例: file1.txt 包含:
This is a file about file.txt
This file is about algorithms.
File.txtbis contains several functions.
There are also several files.
One of the files is sample.c
Another example is test.doc
我希望我的 grep 返回以下行:
This is a file about file.txt
One of the files is sample.c
Another example is test.doc
但是我的 grep 命令没有返回任何内容。
如果我删除了“$”符号,grep 命令会返回文件的所有行,即使它与正则表达式不匹配。
另外,我更愿意过滤掉 1 个或多个字符,而不是 0 个或更多,但 grep 只有 *.我可以在 grep 中使用“+”表示 1 个或多个字符吗?
在反引号中使用 grep 有限制吗?
【问题讨论】:
-
filter out lines that ends with a filename- 你是什么意思?扩大?任何扩展? -
我已经更新了描述。我指的是任何扩展名。
-
将您的
-e修改为-P或-E替代。 -
试试
grep -Er --include=*.txt -e '[a-zA-Z0-9]+[.][a-zA-Z]+$' $dir -n >> test.txt; -
使用
File::Find或File::Find::Rule而不是运行grep。