Reputation: 417
I've a situation where there are different types of files (of different extension) in some of the directories. I've no idea what type of file is present in a particular directory. Let's assume the following case:
There is a common path to all five directories 1, 2, 3, 4, 5 as:
/dir1/dir2/dir3/1/
/dir1/dir2/dir3/2/
/dir1/dir2/dir3/3/
/dir1/dir2/dir3/4/
/dir1/dir2/dir3/5/
In short, 1, 2, 3, 4, 5 are in dir3.
As an example,
directories 1 and 3 have files with extension .txt
directories 2, 4, 5 have files with extension .gz
I'm doing this as of now:
while [ $a -le 5 ]
do
cat /dir1/dir2/dir3/"$a"/*.txt | grep "string" > /path/output"$a"
zcat -f /dir1/dir2/dir3/"$a"/*.gz | grep "string" > /path/output"$a"
((a++))
done
Outputs are created but few of them are blank. I'm sure that inputs have that string I used in grep. I think the script I'm using is over-writing the files.
How can I perform this by searching the file extension first, and performing either cat or zcat accordingly? Please suggest?
Edit:
I've also tried the following:
while [ $a -le 5 ]
do
for f in /dir1/dir2/dir3/"$a"/*.txt
do
cat $f | grep "string" > /path/output"$a"
done
for f in /dir1/dir2/dir3/"$a"/*.gz
do
cat $f | grep "string" > /path/output"$a"
done
done
Upvotes: 0
Views: 71
Reputation: 531055
Collect all the txt files in one array, and if that array is not empty, run cat
. Otherwise, run zcat
. The output of whichever one actually runs gets redirected to the output file.
shopt -s nullglob
for d in 1 2 3 4 5; do
txt=(/dir1/dir2/dir3/"$d"/*.txt)
if (( ${#txt[@]} )); then
grep "string" "${txt[@]}"
else
zcat -f /dir1/dir2/dir3/"$a"/*.gz | grep "string"
fi > /path/output/"$d"
done
Upvotes: 1