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
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

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-24-2007
Registered User
 

Join Date: Oct 2007
Posts: 7
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-24-2007
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,036
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
I hope this helps.

bakunin
Reply With Quote
  #3 (permalink)  
Old 10-25-2007
Registered User
 

Join Date: Oct 2007
Posts: 7
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
Reply With Quote
  #4 (permalink)  
Old 10-25-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
Code:
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.
Reply With Quote
  #5 (permalink)  
Old 01-07-2008
Registered User
 

Join Date: Oct 2007
Posts: 7
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
Reply With Quote
  #6 (permalink)  
Old 01-07-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,449
show how they look like then.
Reply With Quote
  #7 (permalink)  
Old 01-07-2008
Registered User
 

Join Date: Oct 2007
Posts: 7
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
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:48 PM.


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

Content Relevant URLs by vBSEO 3.2.0