We have to identify and extract the date from given samplese
Oct 4 07:44:45 cli[1290]: PAPI_Send: To: 7f000001:8372 Type:0x4 Timed out.
Oct 4 08:16:01 webui[1278]: USER:admin#192.168.100.205 COMMAND:<wlan ssid-profile "MFI-SSID" > -- command executed successfully
Oct 4 08:16:01 webui[1278]: USER:admin#192.168.100.205 COMMAND:<wlan ssid-profile "MFI-SSID" opmode opensystem > -- command executed successfully
Here the main problem is, Date format is versatile. it may be "oct 4 2004" or "oct/04/2004" etc
parsing is best way to handle such a problems.
so learn about parsing techniques then use them on your project and enjoy. Appropriate design pattern for an event log parser?
Related
Is there a script available that can pull the PROGRESS BACKUP STATUS INFO from the log file?
I did a grep on “BACKUP” and it pulls the correct information, but unfortunately there is no time date stamp associated. we are using progress version 9.1D
The end goal would look like this.
Selection Date Range:
Start Date: 07/6/2018
End Date: 7/7/2018
Output:
Date: Mon July 6 20:00 2018
20:03:28 BACKUP 10: Full Backup Started
20:51:44 BACKUP 10: Full Backup Successfully Completed
Date: Mon July 7 20:00 2018
20:03:28 BACKUP 10: Full Backup Started
20:51:44 BACKUP 10: Full Backup Successfully Completed
No. There is no simple "grep" of the log file from version 9 or earlier that will reveal this information.
Prior to version 10 log file entries only contain the time. Not the date. When the day rolls over an entry is written but there are many situations where that date will not be available even for more complex parsing than a simple grep.
Bonus answer: version 9 is ancient, obsolete and unsupported. You should upgrade.
I have a date in a the %c format (could be any other) and I need to use it in the date command. %c is NOT the American format. It is the German one because it's a German server. This also did not work properly on an American server. (Locales set to German or American)
This does not work (error included):
user#server:~$ NOW=$(date +%c); echo $NOW
Do 19 Dez 2013 22:33:28 CET
user#server:~$ date --date="$NOW" +%d/%m/%Y
date: ungültiges Datum „Do 19 Dez 2013 22:33:28 CET“
(date: ungültiges Datum „Do 19 Dez 2013 22:33:28 CET“ = date: invalid date „Do 19 Dez 2013 22:33:28 CET“)
The difficulty is that I don't know which locale or even whci dateformat will be used later since the user can set their own format. So a simple specific parsing solution ist not really going to work!
But how do I do it?
To gerneralize the issue:
If I have a date format format1 (which could be any or at least one that can be reversed) I can use date to get a formatted date. But if I want to format it to another date (format2) how do I do it?
Any solution using anything else than the coreutils is pointless since I am trying to develop a bash script for as many unix machines as possible.
DATE=$(date "+$format1")
date --date="$DATE" "+$format2" # Error in most cases!
This is needed because I have a command which the user can give a date format. This date string is going to be displayed. But in a later step I need to convert this date string into another fixed one. I can manipulate the whcih format the command will get and I can maniplulate the output (or what the user will see).
I cannot run the command twice because it is very time consuming.
Update:
I have found something like a solution:
# Modify $user_format so it can be parsed later
user_format="$user_format %s"
# Time consuming command which will print a date in the given format
output=$(time_consuming_command params "$user_format" more params)
# This will only display what $user_format used to be
echo ${output% *}
# A simple unix timestamp parsing ("${output##* }" will only return the timestamp)
new_formated_date=$(date -d "1970-01-01 ${output##* } sec UTC" "+$new_format")
This is working and might be helpful to others. So I will share this with you.
Not possible with --date as of GNU coreutils 8.22. From the date manual:
‘-d datestr’
‘--date=datestr’
Display the date and time specified in datestr instead of the current
date and time. datestr can be in almost any common format. It can
contain month names, time zones, ‘am’ and ‘pm’, ‘yesterday’, etc. For
example, --date="2004-02-27 14:19:13.489392193 +0530" specifies the
instant of time that is 489,392,193 nanoseconds after February 27,
2004 at 2:19:13 PM in a time zone that is 5 hours and 30 minutes east
of UTC.
Note: input currently must be in locale independent format. E.g., the
LC_TIME=C below is needed to print back the correct date in many
locales:
date -d "$(LC_TIME=C date)"
http://www.gnu.org/software/coreutils/manual/html_node/Options-for-date.html#Options-for-date
Note it says that the input format cannot be in a locale-specific format.
There may be other libraries or programs that would recognize more date formats, but for a given date format it would not be difficult to write a short program to convert it to something date recognizes (for example, with Perl or awk).
Why don't you store the time as unixtime (ie milliseconds since 1st of january 1970) Like 1388198714?
The requested exercise in trying to parse all date formats from all around the world as a one shot bash script without reasonable dependecies is slightly ridiculous.
You may use libdatetime-format-flexible-perl.
#!/usr/bin/perl
use DateTime::Format::Flexible;
my $date_str = "So 22 Dez 2013 07:29:35 CET";
$parser = DateTime::Format::Flexible->new;
my $date = $parser->parse_datetime($date_str);
print $date
Default output will be 2013-12-22T07:29:35, but since $date is not a regular string but object, you can do something like this:
printf '%02d.%02d.%d', $date->day, $date->month, $date->year;
Also date behavior probably should be considered as a bug. I think so, because date in the same format but in russian is parsed correctly.
$ export LC_TIME=ru_RU.UTF-8
$ NOW="$(date "+%c")"
$ date --date="$NOW" '+%d.%m.%Y'
22.12.2013
If you meant the formatting is wrong, I think what you want is:
NOW=$(date +%c)
date --date="$NOW" +%d/%m/%Y
note the lowercase %d and %m.
Locally, this is what I get:
root#server2:~# NOW=$(date +%c)
root#server2:~# date --date="$NOW" +%d/%m/%Y
19/12/2013
I have a log file which logs network activities. I want to view the log file, but I want to see matched pattern. I mean I want to see the content of my choice. The log file is in this format: Nov 7 12:00:00 ......... How can I view content of a specific date or specific time. I mean if I want to see only 3:00 to 5:00 on Nov 7 how can I use the tail command to do that?
There are multiple ways to do what you want. One of them using grep is
grep '^Nov 7 0[3-5]:' network.log | less
Super noob to JavaScript. Would like to create a DateObject, but am still stuck.
Wondering how I can convert a date from toDateString to custom 'MMM DD'.
Tue Mar 09 2014 > Mar 09
*basically, need to parse out the day of the week and the year.
I would like to add any additional objects and functions to this(already converting from UTC):
function dateChange(date) {
return date.toDateString();
}
Thanks in advance; looking forward to learning where I am going wrong.
You could use substr(), although it only makes sense if you're consistently using that same date format.
Or, you could use split():
var splitString = date.toDateString().split(" ");
return splitString[1]+" "+splitString[2];
If you want a more flexible library, check out Datejs (specificially, the FormatSpecifiers would be useful).
I have a date in a the %c format (could be any other) and I need to use it in the date command. %c is NOT the American format. It is the German one because it's a German server. This also did not work properly on an American server. (Locales set to German or American)
This does not work (error included):
user#server:~$ NOW=$(date +%c); echo $NOW
Do 19 Dez 2013 22:33:28 CET
user#server:~$ date --date="$NOW" +%d/%m/%Y
date: ungültiges Datum „Do 19 Dez 2013 22:33:28 CET“
(date: ungültiges Datum „Do 19 Dez 2013 22:33:28 CET“ = date: invalid date „Do 19 Dez 2013 22:33:28 CET“)
The difficulty is that I don't know which locale or even whci dateformat will be used later since the user can set their own format. So a simple specific parsing solution ist not really going to work!
But how do I do it?
To gerneralize the issue:
If I have a date format format1 (which could be any or at least one that can be reversed) I can use date to get a formatted date. But if I want to format it to another date (format2) how do I do it?
Any solution using anything else than the coreutils is pointless since I am trying to develop a bash script for as many unix machines as possible.
DATE=$(date "+$format1")
date --date="$DATE" "+$format2" # Error in most cases!
This is needed because I have a command which the user can give a date format. This date string is going to be displayed. But in a later step I need to convert this date string into another fixed one. I can manipulate the whcih format the command will get and I can maniplulate the output (or what the user will see).
I cannot run the command twice because it is very time consuming.
Update:
I have found something like a solution:
# Modify $user_format so it can be parsed later
user_format="$user_format %s"
# Time consuming command which will print a date in the given format
output=$(time_consuming_command params "$user_format" more params)
# This will only display what $user_format used to be
echo ${output% *}
# A simple unix timestamp parsing ("${output##* }" will only return the timestamp)
new_formated_date=$(date -d "1970-01-01 ${output##* } sec UTC" "+$new_format")
This is working and might be helpful to others. So I will share this with you.
Not possible with --date as of GNU coreutils 8.22. From the date manual:
‘-d datestr’
‘--date=datestr’
Display the date and time specified in datestr instead of the current
date and time. datestr can be in almost any common format. It can
contain month names, time zones, ‘am’ and ‘pm’, ‘yesterday’, etc. For
example, --date="2004-02-27 14:19:13.489392193 +0530" specifies the
instant of time that is 489,392,193 nanoseconds after February 27,
2004 at 2:19:13 PM in a time zone that is 5 hours and 30 minutes east
of UTC.
Note: input currently must be in locale independent format. E.g., the
LC_TIME=C below is needed to print back the correct date in many
locales:
date -d "$(LC_TIME=C date)"
http://www.gnu.org/software/coreutils/manual/html_node/Options-for-date.html#Options-for-date
Note it says that the input format cannot be in a locale-specific format.
There may be other libraries or programs that would recognize more date formats, but for a given date format it would not be difficult to write a short program to convert it to something date recognizes (for example, with Perl or awk).
Why don't you store the time as unixtime (ie milliseconds since 1st of january 1970) Like 1388198714?
The requested exercise in trying to parse all date formats from all around the world as a one shot bash script without reasonable dependecies is slightly ridiculous.
You may use libdatetime-format-flexible-perl.
#!/usr/bin/perl
use DateTime::Format::Flexible;
my $date_str = "So 22 Dez 2013 07:29:35 CET";
$parser = DateTime::Format::Flexible->new;
my $date = $parser->parse_datetime($date_str);
print $date
Default output will be 2013-12-22T07:29:35, but since $date is not a regular string but object, you can do something like this:
printf '%02d.%02d.%d', $date->day, $date->month, $date->year;
Also date behavior probably should be considered as a bug. I think so, because date in the same format but in russian is parsed correctly.
$ export LC_TIME=ru_RU.UTF-8
$ NOW="$(date "+%c")"
$ date --date="$NOW" '+%d.%m.%Y'
22.12.2013
If you meant the formatting is wrong, I think what you want is:
NOW=$(date +%c)
date --date="$NOW" +%d/%m/%Y
note the lowercase %d and %m.
Locally, this is what I get:
root#server2:~# NOW=$(date +%c)
root#server2:~# date --date="$NOW" +%d/%m/%Y
19/12/2013