![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Moving file to directory based on condition. | ramanagh | Shell Programming and Scripting | 2 | 02-02-2008 07:41 AM |
| Extracting data from text file based on configuration set in config file | suparnbector | Shell Programming and Scripting | 3 | 08-09-2007 11:25 PM |
| extract content from a file and insert to another file | fredao | Shell Programming and Scripting | 15 | 12-06-2006 03:36 PM |
| transfer of specific file content to another file | mem101 | Shell Programming and Scripting | 1 | 10-18-2005 11:01 AM |
| find filename based on file content | kollerj | UNIX for Dummies Questions & Answers | 4 | 06-02-2001 10:31 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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.: Code:
#! /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
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 |
|
|||
|
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 |
|
|||
|
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 |
|||
| Google UNIX.COM |