user3181885
user3181885

Reputation: 49

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

Upvotes: 3

Views: 4798

Answers (5)

Jahid
Jahid

Reputation: 22428

Using invert match of grep:

grep -v 'cgs\|CGS' <filelist

Or,

ls | grep -v 'cgs\|CGS'

Upvotes: 0

monk
monk

Reputation: 2115

Following may help you:

ls |grep -iEv "cgs" 

Upvotes: 0

F. Hauri  - Give Up GitHub
F. Hauri - Give Up GitHub

Reputation: 70822

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

Upvotes: 1

agc
agc

Reputation: 8406

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

Upvotes: 1

user1934428
user1934428

Reputation: 22237

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.

Upvotes: 1

Related Questions