Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex grep-E中的正则表达式未按预期工作_Regex_Bash_Shell_Grep - Fatal编程技术网

Regex grep-E中的正则表达式未按预期工作

Regex grep-E中的正则表达式未按预期工作,regex,bash,shell,grep,Regex,Bash,Shell,Grep,我正在编写一个bashshell脚本,它通过一个文件夹,最终根据文件名生成新的目录。 现在,在创建新目录之前,我希望遍历每个文件,去掉路径中不需要的斜杠,忽略文件扩展名。 为了测试这一点,我像这样回显我的文件: #!/bin/sh cpath=`pwd` for file in $cpath/*;do echo $file | grep -E '(?!.*/).+(?=\.)' done 但grep过滤掉了所有东西,我没有得到任何输出。我用RegExr计算出正则表达式 负前瞻匹配最后一个斜杠

我正在编写一个bashshell脚本,它通过一个文件夹,最终根据文件名生成新的目录。 现在,在创建新目录之前,我希望遍历每个文件,去掉路径中不需要的斜杠,忽略文件扩展名。 为了测试这一点,我像这样回显我的文件:

#!/bin/sh

cpath=`pwd`
for file in $cpath/*;do
echo $file | grep -E '(?!.*/).+(?=\.)'
done
但grep过滤掉了所有东西,我没有得到任何输出。我用RegExr计算出正则表达式

负前瞻匹配最后一个斜杠,正前瞻匹配最后一个点

我不确定负前瞻是否是扩展RE的一部分。但是你可以这样做-

#!/bin/bash

for file in $PWD/*; do
basename "${file%.*}"
done 

您甚至不需要
回声
!也可以对$PWD/*中的文件执行
;做
@SiegeX是的!!那将是正确的做法。我将更新答案。lookaheads/lookbehinds不属于ERE。它可以在PCRE中使用,所以grep-P可以在GNUGREP中使用。谢谢你的补充评论。很高兴知道我可以使用lookarounds供以后使用。