Skip to main content
2 of 4
reusing repetitive regex pattern
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

Sundeep
  • 12.2k
  • 3
  • 28
  • 75