grep - search order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep - search order
# 1  
Old 07-15-2009
grep - search order

Hi,

I would like "grep" command to search ALL files in current directory in the order of their modified/created date. How can i do this?

e.g., if 3 files contain the pattern i am searching for, i need the output of "grep" to be in the order such that old file search result should come first.


TIA
Prvn
# 2  
Old 07-15-2009
ls -lt

-t Sort by time modified (most recently modified first) before sort-
ing the operands by lexicographical order.
# 3  
Old 07-15-2009
tell grep about the order.
try:

Code:
ls -lrt  | grep pattern ...

# 4  
Old 07-15-2009
Thanks for your reply. I was actually asking how to change the order of search by "grep" (not "ls"). I need the "grep results" displayed in the order of the files modified/created.
# 5  
Old 07-15-2009
Try this:

Code:
grep 'pattern' $(ls -tr *)

Regards
# 6  
Old 07-15-2009
Thanks Franklin,

It worked great.
# 7  
Old 07-16-2009
Code:
for i in `ls -lt | awk '{print $9}'`;do
        grep pat $i > /dev/null
        if [ $? -eq 0 ];then
        echo $i
        fi
        done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

3. Shell Programming and Scripting

Want to grep records in alphabetical order from a file and split into other files

Hi All, I have one file containing thousands of table names in single column. Now I want that file split into multiple files e.g one file containing table names starting from A, other containing all tables starting from B...and so on..till Z. I tried below but it did not work. for i in... (6 Replies)
Discussion started by: shekhar_4_u
6 Replies

4. Shell Programming and Scripting

Match multiple patterns sequentially in order - grep or awk

Hello. grep v2.21 Debian 8 I wish to search for and output these patterns in order; "From " "To: " "Subject: " "Message-Id: " "Date: " "To: " grep works, but not in strict order... $ grep -a -E "^From |^Subject:|^From: |^Message-Id: |^Date: |^To: " InboxResult; From - Wed Feb 18... (10 Replies)
Discussion started by: DSommers
10 Replies

5. Programming

Gcc linker search path order

I have a build where I wish to link against and load a specific version of a library and a different version of the same library is installed on the system. I'm using a -L option to point to the version that I wish to link against but gcc still seems to choose the installed version. Is there a way... (4 Replies)
Discussion started by: Richard Johnson
4 Replies

6. Shell Programming and Scripting

How to grep lines in the particular order?

Hi guys, I am stuck on a simple issue but couldn't find a simple solution. If you have any ideas please help. I have two files : - FILE1 Tue 09/12 Lindsey Wed 09/13 Randy Thu 09/14 Susan Fri 09/15 Randy Sat 09/16 Lindsey Sun ... (2 Replies)
Discussion started by: sam_2921
2 Replies

7. UNIX for Dummies Questions & Answers

Execute "find . -exec grep" in desending Order

Hi Xperts, I've one query for you. Please help me solving this. Below command is taking long time to fetch names of the files which contain string "475193976" because folder contains millions of files. I agree that this is what this function suppose to do. Correct.. But can it be possible... (6 Replies)
Discussion started by: gav_dhiman
6 Replies

8. Shell Programming and Scripting

PATH dircetory search order

Hello I using CygWin and am working on project that requires whereby after I make some code changes and rebuild I have to manually copy the updated files into the install directory to test them. There is a build output directory where these files placed, but the program will not run from there.... (4 Replies)
Discussion started by: bobban
4 Replies

9. Shell Programming and Scripting

How to grep in order based on the input file

Here is an answer rather than a question that I thought I would share. In my first attempt, I was using grep to find a list from file.1 within another file, file.2 which I then needed to paste the output from file.3 to file.1 in the same order. However, the results weren't what I wanted. At... (2 Replies)
Discussion started by: Kelam_Magnus
2 Replies
Login or Register to Ask a Question