unix scripting to grep the selection date range? - linux

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.

Related

Changing the change timestamp on a file Linux

So I am trying to alter the change timestamp on a file using a script I got from here: Setting/changing the ctime or "Change time" attribute on a file
#!/bin/sh
now=$(date)
echo $now
sudo date --set="qui nov 7 21:05:56 WET 2018"
chmod 777 $1
sudo date --set="$now"
This is the output:
qui nov 8 18:19:39 WET 2018
date: invalid date ‘qui nov 7 21:05:56 WET 2018’
date: invalid date ‘qui nov 8 18:19:39 WET 2018’
What is the matter? The output from date is not a valid date? I tried the fix suggested in the comment to the answer I linked, but it also doesn't work.
AFAIK The reason why the arguments are different:
$ info date
Invoking 'date' with no FORMAT argument is equivalent to invoking it
with a default format that depends on the 'LC_TIME' locale category.
While under Setting the time:
If given an argument that does not start with '+', 'date' sets the
system clock to the date and time specified by that argument (as
described below). You must have appropriate privileges to set the
system clock. Note for changes to persist across a reboot, the hardware
clock may need to be updated from the system clock, which might not
happen automatically on your system.
The argument must consist entirely of digits, which have the
following meaning:
'MM'
month
'DD'
day within month
'hh'
hour
'mm'
minute
'CC'
first two digits of year (optional)
'YY'
last two digits of year (optional)
'ss'
second (optional)
Note, the '--date' and '--set' options may not be used with an
argument in the above format. The '--universal' option may be used with
such an argument to indicate that the specified date and time are
relative to Coordinated Universal Time rather than to the local time
zone.

how to get local date/time in linux terminal while server configured in UTC/different timezone?

how to get local date & time in linux terminal while server configured in UTC or different timezone?
here is what I get now but I'd like to see in local timezone. For eg: PST/PDT.
[jenkins#myServer ~]$ date
Thu Jul 28 18:16:48 UTC 2016
I'm not looking to change system time using hwclock or updating /etc/localtime. Just want to change it for a user.
Also, please let me know - how to persist it for future logins.
Use the TZ environment variable to pass the desired timezone to date:
TZ=<timezone> date
You can find the available timezones in the /usr/share/zoneinfo/ directory and subdirectories. For example, /usr/share/zoneinfo/America/New_York defines TZ=America/New_York.
Example:
$ date
Fri Jul 29 06:31:53 BDT 2016
$ TZ='America/New_York' date
Thu Jul 28 20:31:58 EDT 2016
$ TZ='America/Los_Angeles' date
Thu Jul 28 17:31:54 PDT 2016
For a local time, use "date". For UTC time, use "date -u". Note that, if you use "date" in the server's terminal it returns the server local time.
You can show local time by overriding the TZ environment variable for the process which prints the date. POSIX says a lot about the topic, beginning with
This variable shall represent timezone information. The contents of the environment variable named TZ shall be used by the ctime(), ctime_r(), localtime(), localtime_r() strftime(), mktime(), functions, and by various utilities, to override the default timezone.
Conventional 3-character timezone values were some time ago (more or less) standardized to deprecate the 3-character forms, using the combined standard and daylight savings time form. The preferred form used for PDT is PST8PDT.
There's a page on VMware showing the names and mentioning that they are used on Linux; you may notice that very few of those are 3-character form (aside from the generic UTC+offset).
Further reading:
Valid timeZone Values (VMware):
IANA - Time Zone Database

linux bash - Parse date in custom format

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

Extract Date from text file

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?

Timezone in date?

From where does the date command in Linux get the timezone information?
I cannot see /etc/localtime file and /usr/share/zoneinfo directory in my system. Still when i execute the date command i get the following output
Thu Dec 9 16:28:18 UTC 2010
Kindly tell me from where does the command get the timezone information?
Thanks,
LinuxPenseur
Don't forget that UTC is how standard Unix systems store the date/time in the real time clock. You have to jump through hoops using funny programs (see the hwclock(8) manpage) if you dual-boot to Windows, which prefers the local time to be stored in the CMOS real time clock.
So the date(1) program is simply showing you the results of "I have no configured time zone":
# date -u
Thu Dec 9 10:40:54 UTC 2010
# TZ=UTC date
Thu Dec 9 10:40:57 UTC 2010
# TZ=PST8PDT date
Thu Dec 9 02:41:02 PST 2010
#
From http://www.wikihow.com/Change-the-Timezone-in-Linux
On mobile phones and other small devices that run Linux, the time zone
is stored differently. It is written in /etc/TZ, in the format that is
described, for instance, in [4] . Edit this file manually or use
echo (for instance, echo GMT0BST > /etc/TZ to set the the timezone of
the United Kingdom).
From http://www.radisys.com/files/support_downloads/03245-02_MPCMM0001_MPCMM0002_CMM_Software_TPS.pdf
The CMM determines the offset to local timezone maintained in file
/etc/cmm/TZ and automatically updates the time.
This should help :
http://www.hypexr.org/linux_date_time_help.php

Resources