Search for a file and get the next two files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search for a file and get the next two files
# 1  
Old 02-13-2008
Search for a file and get the next two files

Hi,

My requirement goes like this.

I search for a file using its pattern in a directory. This will be a unique file in that directory. Now i want to get the next two files after the one i find based on time stamp.

e.g.

i search for the file SRSCLAR.IN12345 in a directory.

Now the directory has a list of files. For every SRSCLAR file there will be two corresponding msg and log files. all the 3 have the same time stamp. and these files will appear in this format

Feb 12 15:49 SRSCLAR.IN12345
Feb 12 15:49 invoice_msg_20080212154210.txt
Feb 12 15:49 invoice_log_20080212154210.txt

So i need the next files after SRSCLAR.IN12345 file.

Kindly help. Smilie

Thanks,
Sunny.
# 2  
Old 02-13-2008
Well, if you can guarantee that there are no other files being written to this directory at about that time, this shouldn't be a problem.

Code:
ls -tr1 | awk '/^filename/ && x=NR,NR==(x+2)'

where filename is your SRSC file. If you need to chop off the first entry (SRSC), add "|tail +2" to the end.

If you need more control, I recommend perl's readdir() and stat() routines, which give you exquisite control over reading the contents of the directory.
# 3  
Old 02-13-2008
Code:
#!/bin/sh                                                                                                                           

FILE="SRSCLAR.IN12345"

TS=`find . -name $FILE -printf "%A@\n"`

find . -name "invoice_msg_*.txt" -printf "%p:%A@\n" | grep ":$TS" | cut -f1 -d:
find . -name "invoice_log_*.txt" -printf "%p:%A@\n" | grep ":$TS" | cut -f1 -d:

# 4  
Old 02-15-2008
alternatively:
Code:
ls -tr1| grep -A2 SRSCLAR.IN12345

if you have a grep command which supports the -A flag (gnu grep does).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to search all files for every string in another file

Hello All I have a pattern.txt file in source directory ((/project/source/) in linux server and data looks like: 123abc17 234cdf19 235ifg20 I have multiple log files in log directory (/project/log/) in linux server and data for one log file looks like: <?xml version="1.0" processid... (11 Replies)
Discussion started by: pred55
11 Replies

2. UNIX for Dummies Questions & Answers

Search for string in a file then compare it with excel files entry

All, i have a file text.log: cover6 cover3 cover2 cover4 other file is abc.log as : 0 0 1 0 Then I have a excel file result.xls that contains: Name Path Pass cover2 cover3 cover6 cover4 (1 Reply)
Discussion started by: Anamika08
1 Replies

3. Shell Programming and Scripting

Shell script to search all entries from 1 file to all other separated files.

Hi, I am trying to create a shell script in unix platform, hence will need info on how to start and any ideas from you guys. million thx. Objective: Shell script to search all entries from 1 file(a.out) to all other files and extract the search output to 1 file (c.out). Situation, 1)... (8 Replies)
Discussion started by: Mr_47
8 Replies

4. Red Hat

To search for files of paticular file mask.

how to find files of particular mask from a folder using shell script. for example if i have to find whether files having file mask like FILE%N%.csv ie files like FILE123.csv or files like FILE56.csv are present in the following folder or not. l (2 Replies)
Discussion started by: ramsavi
2 Replies

5. Shell Programming and Scripting

search for content in files. Name of files is in another file. Format as report.

Hi I have multiple files in a folder and one file which contains a list of files (one on each line). I was to search for a string only within these files and not the whole folder. I need the output to be in the form File1<tab>string instance 2<tab> string instance 2<tab>string instance 3... (6 Replies)
Discussion started by: pkabali
6 Replies

6. Shell Programming and Scripting

"Search for files in a file and write these files to another file which sould be in some other direc

Hi , Need to shell script to extracts files names and write those to another file in different directory. input file is inputfile.txt abc|1|bcd.dat 123 david123 123 rudy2345 124 tinku5634 abc|1|def.dat 123 jevid123 123 qwer2345 124 ghjlk5634 abc|1|pqr.txt 123 vbjnnjh435 123 jggdy876... (1 Reply)
Discussion started by: dssyadav
1 Replies

7. UNIX for Dummies Questions & Answers

search a pattern across all file and replace it to all files.

Hi All, HP-UX oradev3 B.11.11 U 9000/800 I have few files, which contain texts. Now I need to search a pattern across all files and replace to that pattern with new one. For example, I have following files they have, which contain something like abc@abc.com, now I need to search text acorss... (6 Replies)
Discussion started by: alok.behria
6 Replies

8. SuSE

Search all files based on first and in all listed files search the second patterns

Hello Linux Masters, I am not a linux expert therefore i need help from linux gurus. Well i have a requirement where i need to search all files based on first patterns and after seraching all files then serach second pattern in all files which i have extracted based on first pattern.... (1 Reply)
Discussion started by: Black-Linux
1 Replies

9. UNIX for Advanced & Expert Users

Search a list of lines in file into files

I have a file (file1) which is like host1 host2 host3 host4 the list goes on............ Now I want the above lines in files to be compared with files under /opt/new/ File names are as below: Dev Prod QA And suppose host1 from file1 is found under Dev(file under /opt/new)... (2 Replies)
Discussion started by: sriram003
2 Replies

10. UNIX for Dummies Questions & Answers

count the number of files which have a search string, but counting the file only once

I need to count the number of files which have a search string, but counting the file only once if search string is found. eg: File1: Please note that there are 2 occurances of "aaa" aaa bbb ccc aaa File2: Please note that there are 3 occurances of "aaa" aaa bbb ccc... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies
Login or Register to Ask a Question