0

I can list all files ls *.nc

 kjkh_lkm_kkkkk_abc_195101-196012.nc
 kjkh_lkm_kkkkk_abc_198101-196012.nc
 kjkh_lkm_kkkkk_bbb_198101-196012.nc
 LM_lkm_kjjkk_abc_196101-196012.nc
 kjkh_lkm_kjjkk_abc_196101-196012.nc
 SAM_lkm_abc_196101-196012.nc
 kjk_lkm_abc_196101-196012.nc

However, I need to extract the parts before _abc ? if more than one, just ignore

The answer below will give:

 kjkh_lkm_kkkkk
 kjkh_lkm_kkkkk_bbb_198101-196012.nc
 LM_lkm_kjjkk
 kjkh_lkm_kjjkk
 SAM_lkm
 kjk_lkm

I need that names that do not includ abc to not appear in the output: desired output:

 kjkh_lkm_kkkkk
 LM_lkm_kjjkk
 kjkh_lkm_kjjkk
 SAM_lkm
 kjk_lkm
bic ton
  • 1,284
  • 1
  • 9
  • 16

1 Answers1

1

Here:

ls *.nc|sed -E 's/(_abc|_bbb).*//'|sort|uniq > newfile
Ron
  • 5,900
  • 2
  • 20
  • 30
  • [Don't use `ls` in scripts.](http://mywiki.wooledge.org/ParsingLs) – tripleee Mar 19 '22 at 11:45
  • The `/g` option is useless because the pattern cannot match more than once per line. – tripleee Mar 19 '22 at 11:45
  • 1
    Redirect to a file with `> filename`. Please search before asking. – tripleee Mar 19 '22 at 11:51
  • I just noticed that if there is a file with a name that does not include abc , this name is returned as is. So how to exclude from the output those do not include abc? – bic ton Mar 19 '22 at 19:45
  • now you are changing the initial question... The answer is that you can use `regex` to catch not only `_abc` but other patterns as well.. – Ron Mar 19 '22 at 20:27
  • Sorry I am not changing. see my update in the question to explain my point, please. – bic ton Mar 19 '22 at 20:57