在bash脚本中使用regex排除某些单词。

时间:2022-09-30 16:21:24

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

我想排除“cgs”和“cgs”,但选择所有其他数据。

Testdata:

Testdata:

exclude this-->

排除这一- - >

C

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.

我试过这样的。*([Cc][Gg][Ss])。*要选择cgs,但是我不理解exclude =)它必须是一个filename_pattern而没有grep。

Kind Regards,

亲切的问候,

Bobby

鲍比

5 个解决方案

#1


1  

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

它必须是一个regexp吗?如果在脚本中设置的话,您可以很容易地使用glob模式来完成

shopt -o extglob

to enable extended globbing. You would then use the pattern

启用扩展的globbing。然后您将使用该模式

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

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

生成名称中没有CGS的所有条目。

#2


1  

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

#3


1  

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

#4


0  

Following may help you:

可以帮助你:

ls |grep -iEv "cgs" 

#5


0  

Using invert match of grep:

使用grep的反匹配:

grep -v 'cgs\|CGS' <filelist

Or,

或者,

ls | grep -v 'cgs\|CGS'

#1


1  

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

它必须是一个regexp吗?如果在脚本中设置的话,您可以很容易地使用glob模式来完成

shopt -o extglob

to enable extended globbing. You would then use the pattern

启用扩展的globbing。然后您将使用该模式

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

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

生成名称中没有CGS的所有条目。

#2


1  

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

#3


1  

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

#4


0  

Following may help you:

可以帮助你:

ls |grep -iEv "cgs" 

#5


0  

Using invert match of grep:

使用grep的反匹配:

grep -v 'cgs\|CGS' <filelist

Or,

或者,

ls | grep -v 'cgs\|CGS'