Sorting a file list returned by grep

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Sorting a file list returned by grep

Post by alex.barylski »

Here is the catch...I need to sort according to last modified time so that oldest files come first...

I have looked at piping grep with sort but I am not sure what switches sort needs to sort by modified date ascending???
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting a file list returned by grep

Post by VladSun »

:P

man ls shows:
-t sort by modification time
Also
--sort=WORD
extension -X, none -U, size -S, time -t, version -v, status -c,
time -t, atime -u, access -u, use -u
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Sorting a file list returned by grep

Post by alex.barylski »

I know ls can do it...but the thing is the files are being returned by grep...
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting a file list returned by grep

Post by VladSun »

PCSpectra wrote:I know ls can do it...but the thing is the files are being returned by grep...
So ;)

Code: Select all

grep "search_string" `ls -t`
This will work in case you don't make recursive grep (i.e. with -R).
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Sorting a file list returned by grep

Post by alex.barylski »

I am doing recursive grep:

Code: Select all

grep -Rl 'Find Me' /var/www/project | sort -M
Is what I was trying...
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting a file list returned by grep

Post by VladSun »

Nice one ;)

Code: Select all

grep "search_string" `find ./ -type f -printf "%A@ %p\n"  | sort -nr | cut -d" " -f2`
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Sorting a file list returned by grep

Post by alex.barylski »

I hope you don't hate me...cause I always appreciate your linux assistance...your my informal GOTO guy whenever I have *nix questions...but

This was actually for a little TODO extractor script I wrote...I wanted to sort the lines based on last modified so that the oldest TODO were addressed first.

Then I decided that a human priority was probably best so I parse the line:

Code: Select all

 
// TODO: This is non-critical TODO task so leave it as default priority
// TODO(123): This is a task that should be done before the previous TODO
 
Using UltraEdit's built in scripting I have essentially given myself full server side searching capabilities that echo results to the Output Window which I can click and automatically find/jump to the line in question...

I do that hundreds of times a day so this will save me tons of time :)

While were on the subject though...

How do I make a PHP script executable from anywhere...

1. Give the script a shebang line -- done!
2. Mark the file as executable X bit -- Not done!
3. Add the path to the script to PATH as environment variable -- Not done!

Are the last two required? Does the last one need to be done each time the OS reboots?

Cheers,
Alex
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting a file list returned by grep

Post by VladSun »

PCSpectra wrote:I hope you don't hate me...cause I always appreciate your linux assistance...your my informal GOTO guy whenever I have *nix questions...but
Thanks :) I don't hate you ;)

newer to older

Code: Select all

grep -h "search_string" `find ./ -type f -printf "%C@ %p\n"  | sort -nr | cut -d" " -f2`
older to newer

Code: Select all

grep -h "search_string" `find ./ -type f -printf "%C@ %p\n"  | sort -n | cut -d" " -f2`
EDIT: Fix: not access time, but status changed time needed
Last edited by VladSun on Wed Oct 08, 2008 2:12 pm, edited 3 times in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting a file list returned by grep

Post by VladSun »

PCSpectra wrote:How do I make a PHP script executable from anywhere...

1. Give the script a shebang line -- done!
2. Mark the file as executable X bit -- Not done!
3. Add the path to the script to PATH as environment variable -- Not done!

Are the last two required? Does the last one need to be done each time the OS reboots?
1. OK
2. You should make it executable.
3. Instead of modifying your PATH variable, try

Code: Select all

ln -s /path/to/your/PHP_script /usr/bin/name_here
or simply copy the PHP file into /usr/bin.

Also, you may edit the .bash_profile of each user's directory, and modify the PATH variable per user.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting a file list returned by grep

Post by VladSun »

Huh?
PCSpectra === Hockey?
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply