Searching Files


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Searching Files
# 1  
Old 10-18-2006
Searching Files

Hi,

I have a file
/db01/dat/march 2006/7001DW06.03B

Please note, between "march 2006" there is a space/tab.

While running the following script, it identifies
/db01/dat/march ----> as first file
2006/7001DW06.03B ---> as second file.


SRC_PATH = /db01/dat
SEARCH_FILENAME = 7001DW06.03B

for i in `find $SRC_PATH -name $SEARCH_FILENAME`
do
IDX=`expr index $i .`

if [ $IDX -ne 0 ]
then
STRTPOS=`expr $IDX - 8`
FILENAME=`expr substr $i $STRTPOS 12`
FILETYPE=`expr substr $FILENAME 5 2`

if [ "$FILETYPE" = "DW" ] || [ "$FILETYPE" = "dw" ]
then
echo Extracting $FILENAME ...
# unzip -jo $i -d $DEST_PATH/$1
fi
fi
done
echo Done

I need to get this as a single file instead of two files.

Regards,
Ronald.
# 2  
Old 10-18-2006
Your code can be written in a simpler way also, if you can provide us your exact requirement, what I understand from your code is that you have to search for files which have "dw or DW" somewhere in their names and then uncompress those files to a specific directory, is it like that?

For your current problem, you can try something like this:
Code:
for i in `find $SRC_PATH -name "$SEARCH_FILENAME"`

# 3  
Old 10-19-2006
Identifying the filles - space in directory name

Hi,

Following is a replication only.

The directory "/db01/dat/march 2006" contains many files like *DW*.03B and others. But in directory name "march 2006" there is a space in between. (While user creating the folder in ftp through internet explorer, they have created with spaces also).

There is a test files like
/db01/dat/march 2006/7001DW06.03B

My sample script is as follows....

SRC_PATH = /db01/dat
SEARCH_FILENAME = *DW06.03B

for i in `find $SRC_PATH -name $SEARCH_FILENAME`
do
echo $i

IDX=`expr index $i .`

if [ $IDX -ne 0 ]
then
STRTPOS=`expr $IDX - 8`
FILENAME=`expr substr $i $STRTPOS 12`
FILETYPE=`expr substr $FILENAME 5 2`

if [ "$FILETYPE" = "DW" ] || [ "$FILETYPE" = "dw" ]
then
echo Extracting $FILENAME ...
# unzip -jo $i -d $DEST_PATH/$1
fi
fi
done
echo Done

While running the above script,
echo $i Displays "/db01/dat/march" and "2006/7001DW06.03B" instead of /db01/dat/march 2006/7001DW06.03B

In /db01/dat/march, the index is zero(0), so i ignore. But in 2006/7001DW06.03B the index is > 0 so I try to extract the file. Since it gets the path($i) as 2006/7001DW06.03B instead of /db01/dat/march 2006/7001DW06.03B it fails.

My requirement is, $i should get /db01/dat/march 2006/7001DW06.03B. Then my problem is solved.

Any good script is most welcome.

Regards,
Ronald.
# 4  
Old 10-19-2006
Did you try:
Code:
for i in `find $SRC_PATH -name "$SEARCH_FILENAME"`

i.e. double quotes around variable name? Try to use double quotes around all variable names.

Still you didn't mention your requirement clearly, you are emphasizing on the space in filename problem but not telling what exactly you are trying to do.

Any ways, try double quotes and see the results.
# 5  
Old 10-19-2006
find files

Hi shereenmotor ..

We have a ftp site. User open this ftp site using intertnet explorer and upload compressed file(these files has a pattern). Sometimes they create folders and while creating folders they use white spaces in between like "march 2006".

While extracting I search for the files and extract. My problem is if they created folder having white spaces I can not exttract using the script given above.

Yes, I tried giving the search variable inside double quotes and still not working.

Regards,
# 6  
Old 10-19-2006
Python alternative:

Code:
#!/usr//bin/python
import os,zipfile
root = "/db01"
SRC_PATH = os.path.join(root,"dat")
SEARCH_FILENAME = "7001DW06.03B"
filetype  = SEARCH_FILENAME[4:6] #DW
for r,d,fi in os.walk(SRC_PATH):
        if fi[0] == SEARCH_FILENAME:
                fullpath = r + os.sep + SEARCH_FILENAME
                z = zipfile.ZipFile(fullpath ,"r")
                for i, name in enumerate(z.namelist()):
                        outfile = open( fullpath + ".extracted", 'wb')
                        outfile.write(z.read(name))
                        outfile.flush()
                        outfile.close()

# 7  
Old 10-19-2006
Quote:
Originally Posted by ronald_brayan
Hi shereenmotor ..

We have a ftp site. User open this ftp site using intertnet explorer and upload compressed file(these files has a pattern). Sometimes they create folders and while creating folders they use white spaces in between like "march 2006".

While extracting I search for the files and extract. My problem is if they created folder having white spaces I can not exttract using the script given above.

Yes, I tried giving the search variable inside double quotes and still not working.

Regards,
Ok, in that case, what we can try to do is that, simply replace each space with a pattern which is extremely unlikely to occur ("___"), run below script to replace white spaces from file and directory names with ("_") before you run your own script.
Code:
#! /bin/ksh

SRC_PATH = /db01/dat

# first, we fix the directories

for name in $(find $SRC_PATH -type d -print | sed 's/ /____/g')
do
  if [ $name != "." ] ; then
    oldname="$(echo $name | sed 's/____/ /g')"
    newname="$(echo $name | sed 's/____/_/g')"
    if [ "$oldname" != "$newname" ] ; then      
      mv "$oldname" "$newname"
    fi
  fi 
done

#fixing individual files...

for name in $(find $SRC_PATH -type f -print | sed 's/ /____/g')
do
  oldname="$(echo $name | sed 's/____/ /g')"
  newname="$(echo $name | sed 's/____/_/g')"
  if [ "$oldname" != "$newname" ] ; then
    mv "$oldname" "$newname"
  fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching files

i want to search a file bt it not happening i m using #!bin/bash read file if (-e "$file") then echo "asfsafafa" else echo "NO SUCH FILE" fi ....error ./VMC.sh: line 5: : command not found NO SUCH FILE ;;;;;;;;;; its giving correctly no such file found but whats is command not found. (7 Replies)
Discussion started by: console
7 Replies

2. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

3. UNIX for Dummies Questions & Answers

Searching all Files on Computer

Hi I need to search all files on my Solaris box and have to know which files are 2GB or more...any combination of commands to do this? (4 Replies)
Discussion started by: mojoman
4 Replies

4. UNIX for Advanced & Expert Users

Searching for files

Hi, I have the following command to list files beginning with a specific name and containing some text... find . -type f -name "dm_merge_domain_adm*" -exec grep -il "Error Message:" '{}' \; -print|xargs ls -ltr It works fine, but seems to list two of each file, when they only exist once...any... (1 Reply)
Discussion started by: chrislluff1976
1 Replies

5. Shell Programming and Scripting

Files searching

I have a list of files in directory and i should write a script if any of these files contains words given in a text file test.txt. the words can be case ignored and word should match. The output should be the name of the directory in which the file is present followed by list of file names Eg:... (1 Reply)
Discussion started by: kinny
1 Replies

6. Shell Programming and Scripting

Searching all files that contain pattern

Hello All, i have to search a pattern in all the files in all subfolders that are present in current directory. suppose i am in d1 directory and in that sd1,sd2,sd3 are subdirectories. in sd1 i have files f1,f2 sd2 i have files f3,f4 sd3 i have file f5 i have to list out all those... (4 Replies)
Discussion started by: ravi.sadani19
4 Replies

7. Shell Programming and Scripting

awk searching between two files

hi there, im writing some script in awk; in few words i have a list from router (mac address- ip address) and the second list with only the mac addresses. the thing is that i want search list from router for the first mac address; if found - print the ip address, if not print error; then search... (1 Reply)
Discussion started by: mac7
1 Replies

8. UNIX for Dummies Questions & Answers

searching for content of files

Hi, This question may be quite newbish. I've stored a few files on my Unix system and am wondering how to search for their contents (i.e. I input the keyword and get a list of files with this keyword) I'd then like to put it on my website (php). I thought of find and grep, but am not... (19 Replies)
Discussion started by: Aretai
19 Replies

9. UNIX for Dummies Questions & Answers

Searching files within subdirectories

I need to concatenate files that are inside a directory and subdirectories. Those files end with .c Can anyone help me I am using comand 9find) and (cat) but they don't work together.:confused: (1 Reply)
Discussion started by: jalvarez
1 Replies

10. UNIX for Dummies Questions & Answers

Searching files..?

hi there can anyone tell me how to search and copy files under unix? im writing shell scripts with 'vi' and 'pico' something like read directoryName if then echo Copying the files copy those *.src files to sub1(another directory) using cp else ... (4 Replies)
Discussion started by: nickaren
4 Replies
Login or Register to Ask a Question