Removing line with common pattern from one file and adding it to another

Viewed 38

I have a series of list files each of which contain a list of filenames. I would like to loop through each of these list files and if one (or more) of its filenames contain a certain common pattern at a given location in the filename I would like to remove it from one of the list files into the other (doesnt matter which list file into which list file). Have to do it in tcsh. For example lets say I have the following 3 list files

file 1:

abcdefg.ext
nyrtsed.ext
sapoltw.ext

file 2:

rncdefg.ext
nydusap.ext

file 3:

revflon.ext
tradefg.ext

and I am looking at the substring from chars 3-6 (0 based) then the filename abcdefg.ext in file 1, has the same substring as the filename rncdefg.ext in file 2 and the same substring as the filename tradefg.ext in file 3. I would like to move those 3 filenames all to the same list file (either file 1, 2 or 3).

So if I moved them all to file 3 I would end up with

file 1:

nyrtsed.ext
sapoltw.ext

file 2:

nydusap.ext

file 3:

revflon.ext
tradefg.ext
abcdefg.ext
rncdefg.ext

This is my first trial (still incomplete) but seems to me to be too complicated (lots of nested for loops)

foreach list1 ($lists)
  foreach file1 ( `cat ${list}`)
    set pat1=`basename $file1 | cut -c10-14`
    foreach list2 ($lists)
       if ($list1 != $list2) then
          foreach file2 ( `cat ${list2}`)
             set pat2=`basename $file2 | cut -c10-14`
             if ( $pat1 == $pat2 ) then
                # have to remove from list1 and add it to list2
                # how to?
             endif
          end
       endif
    end
  end
end
0 Answers
Related