Tuesday, December 20, 2011

Complex find exec zgrep example

Suppose that we have a filesystem containing EDI transactions. Here's an example for the filesystem content:


We need to copy in the current directory all the EDI transactions (text file starting with ISA) from each folders (toto, tata, foo, bar) but the folder must start with a T. In our example we need to copy files from "toto" and "tata". The good transactions are in the folder "good". We must not copy transactions that are not contained in other folder than "good".

Also, the EDI files may be compressed. So we need to verify in compressed files... "toto.gz" contains an EDI file and "tata.xml" is a xml file.

The command is:
find /home/myhome/myfolder/mysubfolder/ -type f -iwholename *good* -iwholename *mysubfolder\/t* -exec zgrep -l '^ISA' {}  \; -exec cp -rf {} . \;

Will copy only "toto.gz"

find => Searching for something
-type f => Looking for files
-iwholename => The path should contains a specific word
-exec zgrep -l '^ISA' {} => Look for file including compressed one. File must start with ISA
-exec cp -rf {} .  => Copy the data to the current directory

No comments:

Post a Comment