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...]
Best Answer
Note that your regex wouldn't work even if
-o
was recognized by yourgrep
implementation. You also need-E
to enable extended regular expressions. Assuming you haveperl
, which you probably do, you can try:But do you really need to make your regex that complicated? Isn't this enough?