Find the answer to your Linux question:
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 ...
  1. #1
    Just 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

  2. #2
    Linux 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...
    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
    Other questions:

    In which directory do these MERIT_nnn_Agreen files exist?

    In which parent directory do you wish for the respective directories to be created?

  3. #3
    Just Joined!
    Join Date
    Jun 2011
    Posts
    2
    Quote Originally Posted by atreyu View Post
    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...
    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
    Other questions:

    In which directory do these MERIT_nnn_Agreen files exist?

    These files currently exist in /labs/tricam/MERIT/Edward/new_evs

    Each set of files in that directory matches the name of a list of directories. So new_evs contains MERIT_010_Agreen, MERIT_010_Bgreen, MERIT_011_Agreen, MERIT_011_Bgreen. The directories respectively for the files to match are /labs/tricam/MERIT/MERIT_010/evs2, /labs/tricam/MERIT/MERIT_011/evs2, etc....

    In which parent directory do you wish for the respective directories to be created?
    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.

  4. #4
    Linux Guru
    Join Date
    May 2011
    Posts
    1,842
    Quote Originally Posted by patze003 View Post
    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.
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...