简体   繁体   中英

regex in bash-script to exclude certain word

I want to exclude "cgs" and "CGS" but select all other data.

Testdata:

exclude this-->

C

SP999_20151204080019_0054236_000_CGS.csv
CSP999_20151204080019_0054236_000_cgs.csv

accept all other.

I tried something like this .*([Cc][Gg][Ss]).* to select the cgs , but I don't understand the exclude thing =) It must be a filename_pattern without grep.

Kind Regards,

Bobby

Does it have to be a regexp? You can easily do it with a glob pattern, if you set in your script

shopt -o extglob

to enable extended globbing. You would then use the pattern

!(*[Cc][Gg][Ss]*)

to generate all entries which do NOT have CGS in their name.

grep --invert-match --ignore-case cgs < filenames_list

extglob option

Try this:

ls -ld $path/!(*[cC][gG][sS].csv)

And have a look at

man -Pless\ +/extglob bash
  If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized. In the following description, a pattern-list is a list of one or more patterns separated by a |. Composite patterns may be formed using one or more of the fol‐ lowing sub-patterns: ?(pattern-list) Matches zero or one occurrence of the given patterns *(pattern-list) Matches zero or more occurrences of the given patterns +(pattern-list) Matches one or more occurrences of the given patterns @(pattern-list) Matches one of the given patterns !(pattern-list) Matches anything except one of the given patterns 

以下可能会帮助您:

ls |grep -iEv "cgs" 

Using invert match of grep:

grep -v 'cgs\|CGS' <filelist

Or,

ls | grep -v 'cgs\|CGS'

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