file moving based on file content

Hi All

my scenario is as follows...

I have the following three files in directory alphabet, containing the respective character string...

Filename Character String
alphabet01.dat AAA
alphabet02.dat BBB
alphabet03.dat CCC

based on the character string within the file, i would like to move the file to another directory, namely

file alphabet01.dat, with character string AAA must move to directory A
file alphabet02.dat, with character string BBB must move to directory B
file alphabet03.dat, with character string CCC must move to directory C

Any assistance in this regard would be greatly appreciated.
thanks
Mel

You didn't say where in the file the character-string is. For the sake of the example I'll assume that "AAA", "BBB", etc. is each the beginning of the first line of the file, which doesn't have any influence on the method, but on the search string.

The method is: extract from every file the character-string to a variable, then decide based upon the content of the variable where the file should go. I assume further that there is a directory /path/to/srcdir where all your files are and a directory "/path/to/tgtdir", which contains directories "/path/to/tgtdir/A", "/path/to/tgtdir/B", etc.:

#! /bin/ksh

typeset fSourceDir="/path/to/srcdir"
typeset fTargetDir="/path/to/tgtdir"
typeset fFile=""
typeset chChar=""

ls -1 "$fSourceDir" | while read fFile ; do
     chChar="$(sed '1 {;s/^\(.\).*$/\1/p;q;}')"
     if [ ! -d $fTargetDir/$chChar ] ; then
          print -u2 "ERROR: processing file $fFile"
          print -u2 "       for Character $chChar there is no corresponding directory in $fTargetDir"
     else
          mv "$fFile" "$fTargetDir/$chChar"
     fi
done

exit 0

I hope this helps.

bakunin

bakunin,

thanks for your assistance.
Just to clarify, the character strings are not at the beginning of the file, but could be anywhere within the file. Also the target directory names are not the same as the character strings, for example.

File Name : G0001.dat
The BMW is a great car, worthy of consideration.

File Name : G0002.dat
The FIAT is your standard run of the mill car

File Name : G0003.dat
The VOLVO is an american vehicle.

these files are all in the same diretory called /data1/motor/report

What i would want to do is...search each file, if i find the character string BMW, move the file to the /data1/motor/german directory. if i find the character string FIAT, move the respective file to the /data1/motor/italian directory. if i find the character string VOLVO, move the respective file to the /data1/motor/usa directory.

Hope this clears it up for you.

thanks
melvyn

for car in BMW:german FIAT:italian VOLVO:usa;do
	IFS=$'\n'
	mv $(grep -l "${car%:*}" data1/motor/report/*.dat) data1/motor/"${car#*:}"
done

Change IFS to IFS='
' if your shell doesn't expand $'\n' to new line.

Hi all,

the latest solution proposed by radoulov seems to work wonderfully, if my files are pure text files, but it appears that they are not pure text file.

There appears to be control characters within the file that is throwing off the grep statement, causing no file names to be returned.

Can anybody me with this.

thanks
melvyn

show how they look like then.

Assuming the files, as shown previously in the post, are as follows....

File Name : G0001.dat
The BMW is a great car, worthy of consideration.^M
This car consists of^M
^M
4 wheels^M
4 Seats^M

File Name : G0002.dat
The FIAT is your standard run of the mill car.^M
It can be used for daily^M
excursions around tow.^M
^M

File Name : G0003.dat
The VOLVO is an american vehicle.^M
This vehicle is common in^M
the following regions^M
^M
California^M
Florida^M
New York^M

Hope this helps

do a dos2unix on the files first before doing other things.
man dos2unix for more info

i am still having problems trying to get this to work.

The grep is having problems returning the filename if there are null charachers within the file. I have tried the dos2unix command, but it still doesn't help.

thanks everybody for their help, based on a combination of all the replies received, i was able to achieve what i wanted.

thanks again
Melvyn

I understand that this is not a helpful post, and I'm glad to hear you have the problem solved... however...

...Volvo is a Swedish made vehicle.

:slight_smile:

Have a nice day.

It was ...

Sorry, my mistake. It was just an example to illustrate the problem i needed solving.