Page 1 of 1

Sorting a file list returned by grep

Posted: Wed Oct 08, 2008 6:02 am
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???

Re: Sorting a file list returned by grep

Posted: Wed Oct 08, 2008 6:17 am
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

Re: Sorting a file list returned by grep

Posted: Wed Oct 08, 2008 6:19 am
by alex.barylski
I know ls can do it...but the thing is the files are being returned by grep...

Re: Sorting a file list returned by grep

Posted: Wed Oct 08, 2008 6:28 am
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).

Re: Sorting a file list returned by grep

Posted: Wed Oct 08, 2008 6:30 am
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...

Re: Sorting a file list returned by grep

Posted: Wed Oct 08, 2008 6:53 am
by VladSun
Nice one ;)

Code: Select all

grep "search_string" `find ./ -type f -printf "%A@ %p\n"  | sort -nr | cut -d" " -f2`

Re: Sorting a file list returned by grep

Posted: Wed Oct 08, 2008 7:25 am
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

Re: Sorting a file list returned by grep

Posted: Wed Oct 08, 2008 7:56 am
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

Re: Sorting a file list returned by grep

Posted: Wed Oct 08, 2008 8:00 am
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.

Re: Sorting a file list returned by grep

Posted: Wed Oct 08, 2008 2:07 pm
by VladSun
Huh?
PCSpectra === Hockey?