1

When I run this command,

grep _rlnAveragePmax *model*

I get this output:

run_ct6_it006_model.star:_rlnAveragePmax                             0.153500
run_ct6_it007_model.star:_rlnAveragePmax                             0.096772
run_it000_model.star:_rlnAveragePmax                             0.000000
run_it001_model.star:_rlnAveragePmax                             0.008995
run_it002_model.star:_rlnAveragePmax                         2.517429e-04
run_it003_model.star:_rlnAveragePmax                             0.003727
run_it004_model.star:_rlnAveragePmax                             0.056681
run_it005_model.star:_rlnAveragePmax                             0.109754
run_it006_model.star:_rlnAveragePmax                             0.153500

But the above output is sorted alphabetically. If we sorted them by time+date created, the output should look like this:

run_it000_model.star:_rlnAveragePmax                             0.000000
run_it001_model.star:_rlnAveragePmax                             0.008995
run_it002_model.star:_rlnAveragePmax                         2.517429e-04
run_it003_model.star:_rlnAveragePmax                             0.003727
run_it004_model.star:_rlnAveragePmax                             0.056681
run_it005_model.star:_rlnAveragePmax                             0.109754
run_it006_model.star:_rlnAveragePmax                             0.153500
run_ct6_it006_model.star:_rlnAveragePmax                             0.153500
run_ct6_it007_model.star:_rlnAveragePmax                             0.096772

What do I have to do to my grep command to get the output sorted by time+date created?

1

Unless you're running macOS, the date a file is created is not stored. That concept is poorly defined anyway, since editing a file often creates a new file that replaces the previous version. What is recorded is the date a file was last modified (which includes both initial creation and any subsequent edit).

The easiest way to sort files by their modification time is to run zsh (as opposed to other, less powerful shells such as bash, ksh or fish). In zsh, you can use glob qualifiers such as Om to sort files by modification time (oldest first, make that Om to get youngest first).

grep _rlnAveragePmax *model*(Om)

That gives the desired result since grep traverses the files in the order given on the command line.

0

First do a time sorted ls, and then pipe the stdout to grep through xargs, using the -H flag to include the filename (since it will do it for only one file, default does not include the filename)

In a first grep after ls, directories are excluded (think this is not a recursive search from the example you provided). A second grep after ls, filters for the filename (without xargs)

ls -tpQ --quoting-style=shell | grep -v / | grep model | xargs -i grep -H _rlnAveragePmax {}

Update after Wildcard's valuable comments: In order to take into account all cases in which filenames may have strange characters, inode numbers are extracted from ls, and piped into "find" for inode number matching and executing grep command. xargs is replaced with "while read line" since there would be two {}'s that the xargs command couldn't differentiate

Symlinks are followed, only files are included, excluding directories etc. The maxdepth argument is redundant in this context (only files are left) however I included it so that a recursive search could also be possible, changing the arguments:

ls -ti | cut -d " " -f1 | while read line; do find -L . -maxdepth 1 -type f -inum $line -exec grep -H xxx {} \; ; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.