Results 1 to 4 of 4
Scripters -
I want to write a simple loop to match partial strings of files and place them into the correct directory based upon the partial string match. The files ...
- 06-29-2011 #1Just Joined!
- Join Date
- Jun 2011
- Posts
- 2
Partial Matching in Loop
Scripters -
I want to write a simple loop to match partial strings of files and place them into the correct directory based upon the partial string match. The files are MERIT_010_Agreen, MERIT_011_Agreen, etc. The directories are MERIT_010, MERIT_011, etc. So I want to match the first 9 characters of the file string. Here is my code, I am having if else problems and I don't think my matching argument is correct.
root_dir=/labs/tricam/MERIT/
subject_dir=`ls /labs/tricam/MERIT/ | grep 'MERIT_[0-9]'`
evs=`ls /labs/tricam/MERIT/Edward/new_evs | grep 'MERIT_[0-9]'`
for subject in ${subject_dir} ; do
if [ ! -d $root_dir/$subject/feat/evs2 ] ; then
mkdir $root_dir/$subject/evs2
for ev in ${evs} ; do
if [ "$( echo $ev | cut -c 1-9)" = ${subject} ] ; then
#ls $ev
cp $ev /$root_dir/$subject/evs2
fi
else
echo "$subject already has evs2"
fi
done
- 06-30-2011 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
Yes, you have some problems with your code, also it is a little confusing trying to understand what you're trying to do.
I've reprinted your code with comments/questions inserted...
Other questions:Code:#!/bin/bash root_dir=/labs/tricam/MERIT/ #subject_dir=`ls /labs/tricam/MERIT/ | grep 'MERIT_[0-9]'` #evs=`ls /labs/tricam/MERIT/Edward/new_evs | grep 'MERIT_[0-9]'` # use the $root_dir variable to be consistent # adding the \{3\} to grep matches on the first 3 numbers subject_dir=`ls $root_dir | grep 'MERIT_[0-9]\{3\}'` evs=`ls ${root_dir}/Edward/new_evs | grep 'MERIT_[0-9]\{3\}'` for subject in ${subject_dir} ; do # why is the 'feat' subdir looked for here, but not used in mkdir below? if ! [ -d ${root_dir}/${subject}/feat/evs2 ] ; then #mkdir $root_dir/$subject/evs2 # add -v to show what mkdir is doing # add -p to allow mkdir to make parent directories mkdir -vp ${root_dir}/${subject}/evs2 # i can't figure out why you're doing this loop for ev in ${evs} ; do if [ "$( echo $ev | cut -c 1-9)" = ${subject} ] ; then #ls $ev #cp $ev /$root_dir/$subject/evs2 # add -v to show what cp is doing cp -v $ev /$root_dir/$subject/evs2 fi # you are missing a done here to close the loop done else echo "$subject already has evs2" fi done
In which directory do these MERIT_nnn_Agreen files exist?
In which parent directory do you wish for the respective directories to be created?
- 06-30-2011 #3Just Joined!
- Join Date
- Jun 2011
- Posts
- 2
See above, basically I wanted to create the new directory in each directory to copy the files to each particular directories evs2 location
In response to the second loop. I want that loop to partial match the first part of the string of the file as indicated above and then copy it to the correct matching location.
- 06-30-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
Okay, how about this?
Code:root_dir=/labs/tricam/MERIT files=$(find ${root_dir}/Edward/new_evs -type f -name 'MERIT_*') for file in $files; do echo -e "\nFound file '$file'" name=$(basename $file) dir=$(echo $name|sed -e 's/^\(MERIT_[0-9]\{3\}\)_.*$/\1/') if [ -n "$dir" ]; then dirname="${root_dir}/${dir}/evs2" [ -d $dirname ] || mkdir -vp $dirname cp -v $file $dirname else echo Failed to match fi done


Reply With Quote
