How to sort the output of grep -l chronologically by newest modification date last?
Try:
ls -rt *.txt | xargs grep -l <pattern>
We first use ls
to list *.txt
files and sort them by modification time (newest last), then for each entry run them through grep
so we only print out files that contain the pattern.
How to sort the output of recursive “grep -lr” chronologically by newest modification date last?
Assuming your file names don't contain newlines:
find dir -type f -printf '%T@\t%p\n' | sort | cut -f2- | xargs grep -l whatever
More robustly using GNU versions of the tools to deal with dir/file names containing exoctic characters:
find dir -type f -printf '%T@\t%p\0' | sort -z | cut -z -f2- | xargs -0 grep -l whatever
How to grep files in date order
You may use this pipeline to achieve this with gnu
utilities:
find . -maxdepth 1 -name '*.py' -printf '%T@:%p\0' |
sort -z -t : -rnk1 |
cut -z -d : -f2- |
xargs -0 grep 'pattern'
This will handle filenames with special characters such as space, newline, glob etc.
find
finds all*.py
files in current directory and prints modification time (epoch value) +:
+ filename +NUL
bytesort
command performs reverse numeric sort on first column that is timestampcut
command removes 1st column (timestamp) from outputxargs -0 grep
command searchespattern
in each file
UNIX - count unique values based on file modification date
There may be better alternatives, but try this one:
ls -lT | tr -s ' ' | cut -d ' ' -f 9 | sort | uniq | wc -l
ls -lT
: Displays detailed listing with modified date and year in full.
tr -s ' '
: Removes excess space
cut -d ' ' -f 9
: Collect the year column
sort
: Sort the years
uniq
: Collect the unique years
wc -l
: Count the number of lines.
Sort logs by date field in bash
For GNU sort: sort -k2M -k3n -k4
-k2M
sorts by second column by month (this way "March" comes before "April")-k3n
sorts by third column in numeric mode (so that " 9" comes before "10")-k4
sorts by the fourth column.
See more details in the manual.
how do sort items in a text file within its contained time interval
Since perl
is available in any unix flavor, and you can use it from command line, I would use it in this way:
$ perl -n -e 'print "$_" if (/(.+)\|(.+)\|/ && $1 eq "14-Oct-2013" && $2 >= "04:18" && $2 <= "05:16")' | sort
14-Oct-2013|04:16|
14-Oct-2013|04:18|
14-Oct-2013|04:20|
14-Oct-2013|05:16|
This could be simplest:
$ perl -n -e 'print $_ if ($_ ge "14-Oct-2013|04:16" && $_ lt "14-Oct-2013|05:17")' | sort
14-Oct-2013|04:16|
14-Oct-2013|04:18|
14-Oct-2013|04:20|
14-Oct-2013|05:16|
To read sh vars you just hast to use correctly quotes and $, so as the shell can expand variables, here you have two cases using single or double quotes to embrace perl arguments:
$ FROM="14-Oct-2013|04:16"
$ TO="14-Oct-2013|05:17"
$ perl -n -e 'print $_ if ($_ ge "'$FROM'" && $_ lt "'$TO'")' | sort
or
$ perl -n -e "print \$_ if (\$_ ge '$FROM' && \$_ lt '$TO')" | sort
Linux grep and sort log files
Try this:
grep --color=always "myID" file*.log | sort -t : -k2,2 -k3,3n -k4,4n
Output:
file3.log:2015-09-26 15:39:48,788 - ERROR - bar : {'id' : myID}
file1.log:2015-09-26 15:39:50,788 - DEBUG - blabla : {'id' : myID}
file2.log:2015-09-26 15:39:51,788 - ERROR - foo : {'id' : myID}
Related Topics
Full Process Name from Task_Struct
Best Way to Build Cross Toolchains on MAC Os X
Sudo User Not Using Same Node Version
Is There a 'ssh-Add' Linux Alpine One Liner
Bash Loop Through Directory Including Hidden File
How to Make Sure Only One Instance of a Bash Script Is Running at a Time
How to Detect If a Server Is Using Spdy
Is There Any Difference Between Executable Binary Files Between Distributions
Tools Required to Learn Arm on Linux X86 Platform
Linux Kernel: Kernel Version String Appended with Either ''+" or "-Dirty"
How to Make Ssh Command Execution to Timeout
Using Su/Sudo When Accessing Remote Git Repositories Over Ssh
Intercepting File System System Calls
Loop Through Array of Arrays of String with Spaces
Visual Studio Code on Linux Xwindow Forwarding
Can't Access Publicly Exposed Docker Container Port from External Machine, Only from Localhost