0

I need help. i have the following in a shell script

FILES_TO_VALIDATE=$FTP_DEST/patternorderresponse_[0-9][0-9][0-9][0-9][0-9][0-9][0-9]_UTC*.xml 

this will get all the files like

patternorderresponse_2023165_UTC1633_0.xml
patternorderresponse_2023165_UTC1633_12.xml
patternorderresponse_2023165_UTC1633_22212.xml

etc.

i would like to exclude the ones that end in _0.xml

How can i do that?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
AnaGutiFontan
  • 19
  • 1
  • 4
  • If it _really is_ bash, `shopt -s extglob` and then you can refer to `...[0-9]_UTC!(*_0).xml`. That doesn't work for `/bin/sh`. – Charles Duffy Jun 20 '23 at 15:22
  • BTW, it's safer to store a list of files in an array. If you use `files_to_validate=( "$FTP_DEST"/... )`, then you can `for file in "${files_to_validate[@]}"; do [[ $file = *_0.xml ]] && continue; ...; done` to store everything in the array but then filter out the things you don't want before acting on them. The big advantage of the array there is that it works correctly with filenames that would mess up regular unquoted glob expansion. – Charles Duffy Jun 20 '23 at 15:26
  • Hello Charles, i have tryed with shopt -s extglob !*_0.xml FILES_TO_VALIDATE=/var/opt/core/env/current/domain/data/save/in/hermes/patternorderresponse_[0-9][0-9][0-9][0-9][0-9][0-9][0-9]_UTC*.xml and is showin the error: line 3: shopt: !*_0.xml: invalid shell option name – AnaGutiFontan Jun 20 '23 at 15:32
  • this works for i in `ls -tr $FILES_TO_VALIDATE` do if [[ $i = *_0.xml ]] then echo $i continue; fi echo next done; – AnaGutiFontan Jun 20 '23 at 16:06
  • thank you very much :) – AnaGutiFontan Jun 20 '23 at 16:06
  • Do see [Why you shouldn't parse the output of `ls`](https://mywiki.wooledge.org/ParsingLs), [BashFAQ #3](https://mywiki.wooledge.org/BashFAQ/003), and [BashFAQ #99](https://mywiki.wooledge.org/BashFAQ/099). – Charles Duffy Jun 20 '23 at 17:40
  • `shopt -s extglob` is a complete command on its own, it doesn't take your glob as a later argument. It just turns on the parser feature that makes `!(*_0)` work, the `!(*_0)` isn't part of the `shopt` command; the glob should be part of a `for` loop or an array assignment or whatever else as appropriate. – Charles Duffy Jun 20 '23 at 17:41
  • Without using _extglob_, I would store the list based on your pattern into an **array** (which makes more sense anyway, if you one day get files with spaces in the name), and then loop over the array and remove the unwanted element. – user1934428 Jun 21 '23 at 09:08

0 Answers0