Searching files in a directory.Urgent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching files in a directory.Urgent
# 1  
Old 12-09-2005
Searching files in a directory.Urgent

Hi everyone,

I need to search files starting with RPT_0, RPT_1,........ in a directory. How can I implement that through a shell script. Also I want to read the last line of each file after searching them. Can someone help me out in this regard.

One more thing how I can extract a particular pattern in the last line of the file.
Eg: ### Job 65 (RPT507A BBW ): 390 lines, 7 pages, 10/26/2005 01:55:16
In the above line I have extract RPT507A BBW and convert that space between RPT507A and BBW with underscore( _ ).

Thanks in advance...
srivsn
# 2  
Old 12-09-2005
Will this help you??

Code:
for i in `ls -1 RPT_*`
do
last=`tail -1 $i`
pat=`expr "$last" : '.*(\(.*\)).*'`
pat=`echo $pat | sed 's/ /_/'`
print $pat
done

# 3  
Old 12-09-2005
Renaming and deleting old file. Pls help

Hi everyone,

Thx a lot Mona for your reply. It worked.

Little modifications. I am extremely new to this Unix. Please help.

After extracting the name from the code below, I want to move the contents of the old file to the new one extracted. We have to delete the old file. Also concatinate the new file name with DD(current day). I will give you a example below.

RPT_1.txt(old filename)
### Job 65 (RPT507A BBW ): 390 lines, 7 pages, 10/26/2005 01:55:16(last line of file RPT_1.txt)

By using the logic given in reply mail, I will get the output as RPT507A_BBW

Now I want to concatinate that with DD(day).
So my output is RPT507A_BBW09 (09 is today)

Also I want to append all the files in RPT_1.txt to new filename RPT507A_BBW09
and delete the old file RPT_1.txt.

The same logic has to be implemented for all the files in the directory starting with RPT_*.txt.

Thanks to all,
srivsn
# 4  
Old 12-09-2005
Code:
day=$(date +%d)
for i in `ls -1 RPT_*`
do
last=`tail -1 $i`
pat=`expr "$last" : '.*(\(.*\)).*'`
pat=`echo $pat | sed 's/ /_/'`
pat=$pat$day
mv $i $pat
done

# 5  
Old 12-09-2005
Parameterising the file pattern

Hi Mona/everyone,

Thx again for Mona Smilie . I need to pass RPT_ as parameter to the shell script which I have below. Say I have created a shell script rn.sh with the code in last reply. Now instead of RPT_ hard coding, I need to give a parameter there and I am using this script in some other job. There I will give the parameter value which includes path name along file name(RPT_). Could you help me in this.....Please give the syntax also.

Thanks,
srivsn
# 6  
Old 12-09-2005
Quote:
Originally Posted by srivsn
Hi Mona/everyone,

Thx again for Mona Smilie . I need to pass RPT_ as parameter to the shell script which I have below. Say I have created a shell script rn.sh with the code in last reply. Now instead of RPT_ hard coding, I need to give a parameter there and I am using this script in some other job. There I will give the parameter value which includes path name along file name(RPT_). Could you help me in this.....Please give the syntax also.

Thanks,
srivsn
This is very simple. Just pass "RPT_" as a parameter to your script. Change the for loop alone like below

Code:
for i in `ls -1 $1*`

# 7  
Old 12-09-2005
Splitting based on some pattern

Hi Mona,

Thx a lot. It worked. Smilie Can you tell me how to split a large file when it came across a pattern like "###" in the beginning of the line.
Eg: ### Job 65 (RPT507A BBW ): 390 lines, 7 pages, 10/26/2005 01:55:16

I have multiple lines like the one above in a single file. I have to split the file into number of files when I came across ### at the beginning of the line and store the lines including the ### line in a temporary file. Please help.

Thx in advance........
srivsn

Last edited by srivsn; 12-11-2005 at 01:02 AM.. Reason: Change in query
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Searching for a files based on current date directory

Hi All, I've been trying to do some recursive searching but not been very successful. Can someone please help. Scenario: I have directory structure /dir1/dir2/dir3/ 2019/ 11/ 17 18 19 20 so what I want to do is run a script and as its 2019/11/18/ today it would go and only search... (3 Replies)
Discussion started by: israr75
3 Replies

2. UNIX for Beginners Questions & Answers

Searching the value of a specific attribute among xmls files from a particular directory location

Hi Folks , I have the different xml files at the following directory `/opt/app/rty/servers/tr/current/ops/config` Let's say there are three files named abc.xml bv.xml ert.xml Now inside these xml there can be many tags as like shown below <bean id="sdrt"... (6 Replies)
Discussion started by: unclesamm
6 Replies

3. Shell Programming and Scripting

Find/searching files in subdirectories excluding the fiels in Parent Directory

Hi All, requirement is to find and remove the files from sub directories but it should exclude the files from parent directory. At present i am using the below one but it finds and remove files from both parent and sub directories. find ${PATH} -type f \( -name securitas\* -o -name \*gz... (1 Reply)
Discussion started by: Naveenkk
1 Replies

4. Shell Programming and Scripting

recursive searching for files in directory that matches a particular name - taking care of links

Hi, I am writing a shell script that finds all files named <myFile> in a directory <dir> or any of its subdirectories, recursively. I also need to take care of symbolic links that may form cycles, to avoid infinite loops. I started writing the code but got stuck. I thought using recursion... (7 Replies)
Discussion started by: vickylife
7 Replies

5. Shell Programming and Scripting

Help needed with searching files and moving them to a different directory

I am new to shell scripting. Can someone help me out with this one please? I need to write a script fot the following scenario: I am currently in /parent directory. I have a set of files in /parent/error_files directory My script has to search for a file in /parent/erratic_files... (1 Reply)
Discussion started by: ss3944
1 Replies

6. Solaris

Searching for files in a Directory

Hi, I am trying to write a script that will search in a particular directory and tell me how many files are in there. I then want to be able to email certain users of how many files are in that directory and what the file names are? any help would be great as i am getting confused. thanks (3 Replies)
Discussion started by: Pablo_beezo
3 Replies

7. UNIX for Dummies Questions & Answers

searching files inside directory

hey, i need to use grep to search a bunch of header files inside a directory to return which file i can find the function i'm searching for in. how do i use wild cards to search through the files? i can only figure out how to search inside the directory, not inside the files that are in the... (4 Replies)
Discussion started by: kylethesir
4 Replies

8. Shell Programming and Scripting

searching files through all subdirectories beneath the current directory

i want to make a bash script that searches a specific pattern in files through all subdirectories beneath the current directory..without using the command grep-R but only the command grep.. e.g for i in * do grep "pattern" $i ..... ... done using the character (*) the script... (5 Replies)
Discussion started by: milagros
5 Replies

9. Shell Programming and Scripting

Searching for files over 30 days old in current directory

May be a simple question for experts here.... I need to get the list of files older than 30 days in the current folder. I tried "find", but it searches recursively in all the sub directories. Can I restrict the recursive search and extract the files only from current directory ? (18 Replies)
Discussion started by: cxredd4
18 Replies
Login or Register to Ask a Question