简体   繁体   中英

How to grep with expansion of curly braces?

I have a list of files that have the nomenclature yyyyMMdd . I would like to get the lsit of the files excluding a few dates. To do so I use the following command

$ ls | grep -ve 202003{04,11,23}
grep: 20200311: Is a directory
grep: 20200323: Is a directory

The command only works if I expand out the curly brace manually, spacing the option -e inbetween, ie

ls | grep -v -e 20200304 -e 20200311 -e 20200323

What would be a more efficient way of doing it?

You can't to this with shell expansion as you want to do because it will replace your single argument with multiple separate arguments (if and only if there are files or directories that match the pattern - if there is no match other undesired things can happen). However, the argument to grep is a regular expression. With extended regular expressions (the -E flag) you can do this.

$ ls | grep -vEe "202003(04|11|23)"

Do note, you don't need -e for a single search parameter, so

$ ls | grep -vE "202003(04|11|23)"

also works.

You may need this: ls | grep -vP "202003(04|11|23)" ls | grep -vP "202003(04|11|23)"

If -P doesn't work, try replace it with -E .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM