Regular Expression usage with ls

戏子无情 提交于 2019-11-26 12:43:51

问题


I am trying to use ER (Extended Regular Expressions) with ls like ls .+\\..+.

I am trying to print all files which contains an extension (I know I could have used ls *.*, but I wanted to try using ER).

When I run that code I get this error: ls: .+..+: No such file or directory.


回答1:


You are confusing regular expression with shell globbing. If you want to use regular expression to match file names you could do:

$ ls | egrep '.+\..+'



回答2:


You don't say what shell you are using, but they generally don't support regular expressions that way, although there are common *nix CLI tools (grep, sed, etc) that do.

What shells like bash do support is globbing, which uses some similiar characters (eg, *) but is not the same thing.

Newer versions of bash do have a regular expression operator, =~:

for x in `ls`; do 
    if [[ $x =~ .+\..* ]]; then 
        echo $x; 
    fi; 
done


来源:https://stackoverflow.com/questions/15345936/regular-expression-usage-with-ls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!