开发者

Bash scripting--regular expressions. regmatch.unix

开发者 https://www.devze.com 2023-03-14 04:20 出处:网络
I am trying to write a bash script(unix) that can compare the files in a folder and match them with files in another folder and detect if matches exists.

I am trying to write a bash script(unix) that can compare the files in a folder and match them with files in another folder and detect if matches exists.

The files in the folders all hav开发者_StackOverflow中文版e a common name structure like :

*****-dbtableName-*****-YYYY-MM-DD.sql  

I have tried [[a-zA-Z]+\DB[A-Z]+\-[a-zA-Z0-9-_]+\.[sql] as a regular expression. Its not working.

I´ll be greatfull for any help.


Have you tried diff?

diff -q directory1 directory2 will report on files that don't exist in both directories or whether they are are identical or not. You can always pipe the results through grep if you want to filter further or use the -x option to exclude particular filetypes from diff. Try info diff for more details on the version on your machine.


Assuming a name like: barbar-fooDB-bazbaz-2011-06-19.sql a match can be analyzed like that:

[a-zA-Z]\+-[a-zA-Z]\+DB-[a-zA-Z]\+-[0-9]\+-[0-9]\+-[0-9]\+\.sql

using eg. grep

$ echo "barbar-fooDB-bazbaz-2011-06-19.sql" | grep "[a-zA-Z]\+-[a-zA-Z]\+DB-[a-zA-Z]\+-[0-9]\+-[0-9]\+-[0-9]\+\.sql"
barbar-fooDB-bazbaz-2011-06-19.sql   # matched

you can optimize it like this

[a-zA-Z-]\+DB-[a-zA-Z0-9-]\+\.sql

which wouldn't be as strict but still matches the correct filename

$ echo "barbar-fooDB-bazbaz-2011-06-19.sql" | grep "[a-zA-Z-]\+DB-[a-zA-Z0-9-]\+\.sql"
barbar-fooDB-bazbaz-2011-06-19.sql   # matched

or if you prefer this example

$ touch /tmp/barbar-fooDB-bazbaz-2011-06-19.sql
$ ls -1 /tmp/ | grep "[a-zA-Z-]\+DB-[a-zA-Z0-9-]\+\.sql"

This is all good for dealing with the regex, but for your problem it maybe better to take Dan's advice and use diff combining it with a wildcard like *.sql or whatever fits your needs.

0

精彩评论

暂无评论...
验证码 换一张
取 消