3

I am trying to extract the date and timestamps from my log file string ($Data in the below code) on AIX using a regex and loading it into a text file as below:

Data="Logs/2018-12-03/log.txt:3:2018-12-03 00:00:04,333 452621453 [blah blah blah"
echo "$data" | grep -o -n '[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]' >  timestamps.txt

I get the below error most probably due to the unix version in my host being too old and it doesn't recognize the -o option. Is there any alternative method in which I can get the functionality of -o done?

grep: Not a recognized flag: o
Usage: grep [-r] [-R] [-H] [-L] [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] -e pattern_list...
    [-f pattern_file...] [file...]
Usage: grep [-r] [-R] [-H] [-L]  [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] [-e pattern_list...]
    -f pattern_file... [file...]
Usage: grep [-r] [-R] [-H] [-L] [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] pattern_list [file...]
  • What operating system are you using? – terdon Dec 4 '18 at 9:16
  • The command uname -s gives me 'AIX' – BlueNinja Dec 4 '18 at 9:21
  • 1
    It's not that your Unix is "too old", it's that you are relying on non-standard functionality. – Kusalananda Dec 4 '18 at 9:24
  • Is your Data variable a line from the output of grep? If it is, you can probably do whatever you are doing a fair bit more efficiently by running perl or awk over the original log files. Parsing the output of grep is not an ideal solution. – Kusalananda Dec 4 '18 at 10:41
5

Note that your regex wouldn't work even if -o was recognized by your grep implementation. You also need -E to enable extended regular expressions. Assuming you have perl, which you probably do, you can try:

$ Data="Logs/2018-12-03/log.txt:3:2018-12-03 00:00:04,333 452621453 [blah blah blah"
$ echo "$Data" | perl -lne '/[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]/ && print "$.:$&"'
1:2018-12-03 00:00

But do you really need to make your regex that complicated? Isn't this enough?

$ echo "$Data" | perl -lne '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}/ && print "$.:$&"'
1:2018-12-03 00:00
  • 1
    Note also that the data in the Data variable is a line produced by grep. This makes me think that the user is first running a grep of some kind to get the lines they want, and then iterating in a shell loop over these lines. This is quite an suboptimal approach to log file parsing. – Kusalananda Dec 4 '18 at 10:43

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.