Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Linux and Unix
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
comparing values in a file to values in a file's name.

Reply  
Thread Tools
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
18-Oct-2005, 12:18 PM #1
comparing values in a file to values in a file's name.
Okay, here is the question.

How do I parse a file with 'codes' in them, and based on file names in a directory, if the file name also has the same code, move that file to another directory.

The files name would be 02gr_text_CODE123_stuff.dat. There may be more than one file in this directory, so also another file by the name of 05_gr_text_CODE987 or 76gr_new_CODE456_morestuff.dat. The codes in the file would be CODE123 and CODE456.

Here is what I have come up with as a starting point...


val_file=/directory/err_campcd

while read -r line
do
for file in *
do
cd /codedirectory

code1=`ls | grep gr_text | cut -b3- | sed -e 's/gr_text_//g' | cut -c1-7`
code2=`ls | grep gr_new | cut -b3- | sed -e 's/gr_new_//g' | cut -c1-7`

if ${code1} -eq ${line}
then
echo "moving code1 rejected files"
elseif ${code2} -eq ${line}
echo "moving code2 rejected files"
else
echo "no rejected files"
fi


done

done < $val_file
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
18-Oct-2005, 02:56 PM #2
here is what i came up with that works somewhat the way i need it to... id have to do one of these code blocks for each different file name but at least it works.

for file in `ls *gr_text*`
do
code=`echo $file | cut -b3- | sed -e 's/gr_text_//g' | cut -c1-7`
while read -r line
do
if [ ${line} = ${code} ]
then
echo "moving rejected files"
else
echo "no rejected files"
fi
done < $val_file
done
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
18-Oct-2005, 03:53 PM #3
Your second example is pretty I/O-hungry. Assuming I understand your need correctly, here's my try:

destination=/this_is_where_the_files_will_be_moved_to
val_file=/directory/err_campcd

while read code
do

for file in ??gr_text${code}_*.dat
do
echo "moving $file"
mv $file $destination
done

done < $val_file

#learn to use your file-name metacharacters; they'll make your life easier!

If you don't mind error messages for nonexistent files, this is even simpler:

for code in $(<$val_file)
do
mv ??gr_text${code}_*.dat $destination
done
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
18-Oct-2005, 05:36 PM #4
yup... after walking away from what i was doing and coming back to it an hour or so after, i realized i was going way more complicated then i needed to be.. here is what i came up with, somewhat similar i think to your last suggestion.

while read -r line
do
mv -f *${line}*.* /directory/.
done < $val_file
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
18-Oct-2005, 06:18 PM #5
There ya go!
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:55 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.