Separate based on file names

Hello experts,
This might prove to be a stupid question to some of you, but I have tried to tackle it in different ways. Being new to shell scripting, I am requesting your help in coming up with an elegant solution. I am using Korn shell.

We have a directory with file names with the pattern:
abc_123
abc_456
xyz_789

I have to compare them against a prefix file to check whether the names behind the underscore are valid and consolidate them in different directories.

How do I do that?

  1. Put them in different files and use diff/comm to compare. This requires multiple passes - since the consolidation is more than what I can highlight here.
  2. Do something like:
    FileList=$(ls -r $SOURCE_DIR) # list & sort
    PrefixList=$(cat $PREFIX_FILE | sort -r)
    for sFile in $sFileList
    do
    iDelimit=$(expr index $sFile _)
    (( iDelimit -= 1 ))
    sFileCmp=$(expr substr $sFile 1 $iDelimit)
    for sPrefix in $sPrefixList
    do
    echo $sFileCmp compared with $sPrefix
    if [ $sFileCmp -gt sPrefix ] # will not work, symbolic
    then
    continue
    elif [ $sFileCmp -eq sPrefix ]
    then
    blPrefixValid=1
    else
    blPrefixValid=0
    break
    fi
    done
  3. Get valid prefixes and run through the directory each time to process files

Any help appreciated.

Thanks.

What is the criteria to separate the files?

Thanks
Nagarajan G

Thanks for your reply.

The prefix is the criteria. If the file has valid prefix (that can be found in the prefix list) - that goes in one directory, if it is a duplicate it goes in the other and so on.