Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

Sample input modified to get one matching line in output

$ cat ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
90354 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2014-03-05 16:00 /path/dir/dir2/file1
89743 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2010-03-05 16:00 /path/dir/dir2/file2

$ grep -P '^(\S+\s+){2}-(?1){5}(\d\d(0\d|1[0-2]))(?1){2}(?2)' ip.txt 
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
  • (\S+\s+) non-space text followed by white-space. {5} or {2} tells it to be repeated that many times
  • \d\d(0\d|1[0-2]) for years 2012 or prior (assuming first two digits in year doesn't exceed 20)
  • (?1) refers to (\S+\s+) and (?2) refers to (\d\d(0\d|1[0-2]))

`perl` solution similar to [glenn's answer](httphttps://unix.stackexchange.com/a/311156/109046)
$ perl -ae 'print if $F[2] =~ /^-/ && $F[7] < 2013 && $F[9] < 2013' ip.txt 
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2

Add `> output.txt` to end of command to save the results to another file

Sample input modified to get one matching line in output

$ cat ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
90354 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2014-03-05 16:00 /path/dir/dir2/file1
89743 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2010-03-05 16:00 /path/dir/dir2/file2

$ grep -P '^(\S+\s+){2}-(?1){5}(\d\d(0\d|1[0-2]))(?1){2}(?2)' ip.txt 
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
  • (\S+\s+) non-space text followed by white-space. {5} or {2} tells it to be repeated that many times
  • \d\d(0\d|1[0-2]) for years 2012 or prior (assuming first two digits in year doesn't exceed 20)
  • (?1) refers to (\S+\s+) and (?2) refers to (\d\d(0\d|1[0-2]))

`perl` solution similar to [glenn's answer](http://unix.stackexchange.com/a/311156/109046)
$ perl -ae 'print if $F[2] =~ /^-/ && $F[7] < 2013 && $F[9] < 2013' ip.txt 
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2

Add `> output.txt` to end of command to save the results to another file

Sample input modified to get one matching line in output

$ cat ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
90354 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2014-03-05 16:00 /path/dir/dir2/file1
89743 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2010-03-05 16:00 /path/dir/dir2/file2

$ grep -P '^(\S+\s+){2}-(?1){5}(\d\d(0\d|1[0-2]))(?1){2}(?2)' ip.txt 
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
  • (\S+\s+) non-space text followed by white-space. {5} or {2} tells it to be repeated that many times
  • \d\d(0\d|1[0-2]) for years 2012 or prior (assuming first two digits in year doesn't exceed 20)
  • (?1) refers to (\S+\s+) and (?2) refers to (\d\d(0\d|1[0-2]))

`perl` solution similar to [glenn's answer](https://unix.stackexchange.com/a/311156/109046)
$ perl -ae 'print if $F[2] =~ /^-/ && $F[7] < 2013 && $F[9] < 2013' ip.txt 
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2

Add `> output.txt` to end of command to save the results to another file
added perl solution
Source Link
Sundeep
  • 12.2k
  • 3
  • 28
  • 75

Sample input modified to get one matching line in output

$ cat ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
66738 8 drwx-rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 20152011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
90354 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2014-03-05 16:00 /path/dir/dir2/file1
89743 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2010-03-05 16:00 /path/dir/dir2/file2

$ grep -P '^(\S+\s+){72}-(?1){5}(\d\d(0\d|1[0-2]))(\S+\s+?1){2}\d\d(0\d|1[0-2]?2)' ip.txt 
2366738 8 drwx-rwx--S--- 163 userid grpid 20481024 20092010-0603-2505 2011:4249 2011-0307-0307 17:2748 20112010-03-0305 1711:2749 /path/dir/dir2
  • (\S+\s+) non-space text followed by white-space. {75} or {2} tells it to be repeated that many times
  • \d\d(0\d|1[0-2]) for years 2012 or prior (assuming first two digits in year doesn't exceed 20)
  • (?1) refers to (\S+\s+) and (?2) refers to (\d\d(0\d|1[0-2]))

Since we have repeating regex patterns, we can refine it further:


`perl` solution similar to [glenn's answer](http://unix.stackexchange.com/a/311156/109046)
grep$ perl -Pae '^(\S+\s+){7}(\d\d(0\d|1[0'print if $F[2] =~ /^-2]))(?1){2}(?2)'/ && $F[7] < 2013 && $F[9] < 2013' ip.txt 

where (?1) refers to (\S+\s+) and (?2) refers to (\d\d(0\d|1[0-2]))


If -P is not an option, use -E

grep66738 -E8 '^(\S+\s+){7}[0-9][0rwx-9](0[0-9]|1[0S-2])(\S+\s+){2}[0-9][0-9](0[0 3 userid grpid 1024 2010-9]|1[003-2])'05 ip.txt11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2

Add > output.txt to end of command to save the results to another file

Add `> output.txt` to end of command to save the results to another file
$ cat ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
66738 8 drwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2015-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
90354 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2014-03-05 16:00 /path/dir/dir2/file1
89743 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2010-03-05 16:00 /path/dir/dir2/file2

$ grep -P '^(\S+\s+){7}\d\d(0\d|1[0-2])(\S+\s+){2}\d\d(0\d|1[0-2])' ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
  • (\S+\s+) non-space text followed by white-space. {7} or {2} tells it to be repeated that many times
  • \d\d(0\d|1[0-2]) for years 2012 or prior (assuming first two digits in year doesn't exceed 20)

Since we have repeating regex patterns, we can refine it further:

grep -P '^(\S+\s+){7}(\d\d(0\d|1[0-2]))(?1){2}(?2)' ip.txt 

where (?1) refers to (\S+\s+) and (?2) refers to (\d\d(0\d|1[0-2]))


If -P is not an option, use -E

grep -E '^(\S+\s+){7}[0-9][0-9](0[0-9]|1[0-2])(\S+\s+){2}[0-9][0-9](0[0-9]|1[0-2])' ip.txt 

Add > output.txt to end of command to save the results to another file

Sample input modified to get one matching line in output

$ cat ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
90354 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2014-03-05 16:00 /path/dir/dir2/file1
89743 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2010-03-05 16:00 /path/dir/dir2/file2

$ grep -P '^(\S+\s+){2}-(?1){5}(\d\d(0\d|1[0-2]))(?1){2}(?2)' ip.txt 
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
  • (\S+\s+) non-space text followed by white-space. {5} or {2} tells it to be repeated that many times
  • \d\d(0\d|1[0-2]) for years 2012 or prior (assuming first two digits in year doesn't exceed 20)
  • (?1) refers to (\S+\s+) and (?2) refers to (\d\d(0\d|1[0-2]))

`perl` solution similar to [glenn's answer](http://unix.stackexchange.com/a/311156/109046)
$ perl -ae 'print if $F[2] =~ /^-/ && $F[7] < 2013 && $F[9] < 2013' ip.txt 
66738 8 -rwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2011-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
Add `> output.txt` to end of command to save the results to another file
reusing repetitive regex pattern
Source Link
Sundeep
  • 12.2k
  • 3
  • 28
  • 75
$ cat ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
66738 8 drwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2015-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
90354 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2014-03-05 16:00 /path/dir/dir2/file1
89743 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2010-03-05 16:00 /path/dir/dir2/file2

$ grep -P '^(\S+\s+){7}\d\d(0\d|1[0-2])(\S+\s+){2}\d\d(0\d|1[0-2])' ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
  • (\S+\s+) non-space text followed by white-space. {7} or {2} tells it to be repeated that many times
  • \d\d(0\d|1[0-2]) for years 2012 or prior (assuming first two digits in year doesn't exceed 20)

Since we have repeating regex patterns, we can refine it further:

grep -P '^(\S+\s+){7}(\d\d(0\d|1[0-2]))(?1){2}(?2)' ip.txt 

where (?1) refers to (\S+\s+) and (?2) refers to (\d\d(0\d|1[0-2]))


If -P is not an option, use -E

grep -E '^(\S+\s+){7}[0-9][0-9](0[0-9]|1[0-2])(\S+\s+){2}[0-9][0-9](0[0-9]|1[0-2])' ip.txt 
 

Add > output.txt to end of command to save the results to another file

$ cat ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
66738 8 drwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2015-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
90354 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2014-03-05 16:00 /path/dir/dir2/file1
89743 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2010-03-05 16:00 /path/dir/dir2/file2

$ grep -P '^(\S+\s+){7}\d\d(0\d|1[0-2])(\S+\s+){2}\d\d(0\d|1[0-2])' ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
  • (\S+\s+) non-space text followed by white-space. {7} or {2} tells it to be repeated that many times
  • \d\d(0\d|1[0-2]) for years 2012 or prior (assuming first two digits in year doesn't exceed 20)

If -P is not an option, use -E

grep -E '^(\S+\s+){7}[0-9][0-9](0[0-9]|1[0-2])(\S+\s+){2}[0-9][0-9](0[0-9]|1[0-2])' ip.txt 

Add > output.txt to end of command to save the results to another file

$ cat ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
66738 8 drwx--S--- 3 userid grpid 1024 2010-03-05 11:49 2015-07-07 17:48 2010-03-05 11:49 /path/dir/dir2
90354 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2014-03-05 16:00 /path/dir/dir2/file1
89743 8 -rw-r----- 1 userid grpid 65536 2016-05-25 15:28 2008-05-22 12:00 2010-03-05 16:00 /path/dir/dir2/file2

$ grep -P '^(\S+\s+){7}\d\d(0\d|1[0-2])(\S+\s+){2}\d\d(0\d|1[0-2])' ip.txt 
23 8 drwx------ 16 userid grpid 2048 2009-06-25 20:42 2011-03-03 17:27 2011-03-03 17:27 /path/dir
  • (\S+\s+) non-space text followed by white-space. {7} or {2} tells it to be repeated that many times
  • \d\d(0\d|1[0-2]) for years 2012 or prior (assuming first two digits in year doesn't exceed 20)

Since we have repeating regex patterns, we can refine it further:

grep -P '^(\S+\s+){7}(\d\d(0\d|1[0-2]))(?1){2}(?2)' ip.txt 

where (?1) refers to (\S+\s+) and (?2) refers to (\d\d(0\d|1[0-2]))


If -P is not an option, use -E

grep -E '^(\S+\s+){7}[0-9][0-9](0[0-9]|1[0-2])(\S+\s+){2}[0-9][0-9](0[0-9]|1[0-2])' ip.txt 
 

Add > output.txt to end of command to save the results to another file

Source Link
Sundeep
  • 12.2k
  • 3
  • 28
  • 75
Loading