file moving based on file content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file moving based on file content
# 1  
Old 10-24-2007
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  
Old 10-24-2007
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  
Old 10-25-2007
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  
Old 10-25-2007
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  
Old 01-07-2008
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  
Old 01-07-2008
show how they look like then.
# 7  
Old 01-07-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to create file and file content based existing information?

Hi Gurus, I am SQL developer and new unix user. I need to create some file and file content based on information in two files. I have one file contains basic information below file1 and another exception file file2. the rule is if "zone' and "cd" in file1 exists in file2, then file name is... (13 Replies)
Discussion started by: Torhong
13 Replies

2. Shell Programming and Scripting

Moving files based on file name

Hi All, I have multiple files in the folder, I want to move those files into the other folder on based of name File names: Template_server1_01==> Template_server1_02==>To one directory /Server1 Template_server1_03==> Template_server2_01==> Template_server2_02==>To one... (9 Replies)
Discussion started by: sharsour
9 Replies

3. Shell Programming and Scripting

Script for moving one file with date content in name

Dear All, I am having a database that generates daily backup files in the below format. abc_date_0010.zip. Can you please suggest a method to copy the last day's file to another location. I am trying with if statement, but having difficulty while I use the condition like if ; then ... (4 Replies)
Discussion started by: skooby
4 Replies

4. Shell Programming and Scripting

Shell Script to Dynamically Extract file content based on Parameters from a pdf file

Hi Guru's, I am new to shell scripting. I have a unique requirement: The system generates a single pdf(/tmp/ABC.pdf) file with Invoices for Multiple Customers, the format is something like this: Page1 >> Customer 1 >>Invoice1 + invoice 2 >> Page1 end Page2 >> Customer 2 >>Invoice 3 + Invoice 4... (3 Replies)
Discussion started by: DIps
3 Replies

5. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

6. Shell Programming and Scripting

Remove the file content based on the Header of the file

Hi All, I want to remove the content based on the header information . Please find the example below. File1.txt Name|Last|First|Location|DepId|Depname|DepLoc naga|rr|tion|hyd|1|wer|opr Nava|ra|tin|gen|2|wera|opra I have to search for the DepId and remove the data from the... (5 Replies)
Discussion started by: i150371485
5 Replies

7. Shell Programming and Scripting

Moving a file based on size

hello, I am creating a script that is going to scan the contents of a directory, and if it finds a certain DocumentName that is a certain size (The example, DocumentName and a size of 8777) If this file exists, it needs to be moved to a directory for later removal/processing..anyone have... (7 Replies)
Discussion started by: jeffs42885
7 Replies

8. UNIX for Dummies Questions & Answers

Changing file content based on file header

Hi, I have several text files each containing some data as shown below: File1.txt >DataHeader Data... Data... File2.txt >DataHeader Data... Data... etc. What I want is to change the 'DataHeader' based on the file name. So the output should look like: File1.txt >File1 ... (1 Reply)
Discussion started by: Fahmida
1 Replies

9. Shell Programming and Scripting

Moving Content from one file to another with variables

Greetings All, Need some help, your input is appreciated. Scenario: I have five files, each has one line with comletely different content. I execute the command to combine all the five lines of content into a single file. Goal: I would like to take that single file that has the... (3 Replies)
Discussion started by: royarellano
3 Replies

10. UNIX for Advanced & Expert Users

Reading a file and sending mail based on content of the file

Hi Gurus, I am having an requirement. i have to read a list file which contains file names and send mail to different users based on the files in the list file. eg. if file a.txt exists then send a mail to a@a.com simillary for b.txt,c.txt etc. Thanks for your help, Nimu (6 Replies)
Discussion started by: nimu1979
6 Replies
Login or Register to Ask a Question