The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Break a file into separate files chiru_h Shell Programming and Scripting 8 07-29-2008 12:06 AM
File names based off of wildcards tcquad UNIX for Dummies Questions & Answers 1 01-30-2008 02:51 PM
Output of command to 2 separate file? TheCrunge UNIX for Dummies Questions & Answers 2 12-08-2005 01:45 PM
Separate a portion of text file into another file srikanth_ksv Shell Programming and Scripting 4 08-05-2005 05:11 AM
Listing words from a file on a Separate Line Astudent UNIX for Dummies Questions & Answers 2 03-14-2001 03:44 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 2
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.
Reply With Quote
Forum Sponsor
  #2  
Old 02-04-2008
Registered User
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
Quote:
Originally Posted by prashk15 View Post
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
Reply With Quote
  #3  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 2
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:27 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0