The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 11:41 AM
Extracting data from text file based on configuration set in config file suparnbector Shell Programming and Scripting 3 08-10-2007 02:25 AM
extract content from a file and insert to another file fredao Shell Programming and Scripting 15 12-06-2006 07:36 PM
transfer of specific file content to another file mem101 Shell Programming and Scripting 1 10-18-2005 02:01 PM
find filename based on file content kollerj UNIX for Dummies Questions & Answers 4 06-02-2001 01:31 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-24-2007
melvyn.cochrane melvyn.cochrane is offline
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
  #2 (permalink)  
Old 10-24-2007
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
  #3 (permalink)  
Old 10-25-2007
melvyn.cochrane melvyn.cochrane is offline
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
  #4 (permalink)  
Old 10-25-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
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.
  #5 (permalink)  
Old 01-07-2008
melvyn.cochrane melvyn.cochrane is offline
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
  #6 (permalink)  
Old 01-07-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
show how they look like then.
  #7 (permalink)  
Old 01-17-2008
melvyn.cochrane melvyn.cochrane is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 7
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0