0

I have a folder with some sql files and I want to import everything with a script using sqlplus. Inside the folder I do this and it works, converted all the files into a single one:

for filename in $(ls *.sql);
do
    cat $filename >> all.sql
done

But my problem is, some of this sql files had "ADM" on the title, and I want to put this files into other file, because they have a different user/pass on DB. So, what i need is:

for filename in $(ls *ADM*.sql);
do
    cat $filename >> all_ADM.sql
done

for filename in --Rule that catch all files with ".sql" but without "ADM" in the title--
do
    cat $filename >> all.sql
done

I'm not so good with regex and I already try a lot of things that I found on the internet, but without success. Thanks!!

  • 1
    First, instead of using a loop, you can do `cat *.sql > all.sql`. – pawamoy Apr 11 '18 at 17:20
  • And never do `for file in $(ls *.sql)`; the pattern is `for file in *.sql` plain and simple. See also [useless use of `ls`](http://www.iki.fi/era/unix/award.html#ls) and https://mywiki.wooledge.org/ParsingLs – tripleee Apr 11 '18 at 17:24

1 Answers1

1

You can use grep -v to exclude all files that match *ADM*.sql:

for filename in $(ls *.sql | grep -v "*ADM*.sql");
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56