How to convert date to timestamp in bash shell? - bash

I need to write a script to check one of my log files to check its last updated time. If it's greater than 90 minutes, I need to get alerted. But now I am stuck in timestamp conversion.
I have the below line in my code, but it's not working. It's throwing an error like ./monitor_log.sh: line 13: Aug: command not found.
modifeidTimestamp=$(date --date="`$lastModified`" +"%s");
The command below is working fine, but I have to use a variable. Can anyone help?
date --date='Aug 25 06:07:30' +"%s"

You can do:
lastModified='Aug 25 06:07:30'
modifeidTimestamp=$(date --date="$lastModified" +"%s")
echo "$modifeidTimestamp"
1440497250
No need to use reverse tick around lastModified variable. Use wrap it in double quotes.

Related

Count the String occurance by extracting the data from log file in specified range of time

I am new to shell scripting and my problem is:-
I wanted to get the count of a string that is occurring in the log file. Since the log file always get updated in every seconds so I need the data of previous 15 minute only not the whole log file.
Lets say for example if I have a log file name I_am_Log and it use to get updated in every second and I have a string called "Hey, How R U" and I want to get the count of this string that is occurring in the log but only in the last 15 minutes.
I don't know a Unix shell scripting that much, so I am not posting any of my effort.
I can do it by using the following the command:-
grep -i "Hey, How R U" /path/to/file | wc -l
but this command will show the output from the whole file but I just want the output of previous 15 mins...

Error while extracting data from log file

I am running awk command to extract data from log file to calculate last 15 minutes log using below command and now i am getting bellow error:
awk '$0>=$from' from=$(`date -u +"####<%d-%b-%Y %H:%M:%S o'clock GMT>"-15min`) test.log
Error:
date: 0551-402 Invalid character in date/time specification.
Usage: date [-u] [+"Field Descriptors"]
awk: 0602-562 Field $() is not correct.
The input line number is 1. The file is test.log.
The source line number is 1.
Can anyone see what the problem is?
I believe you mean
awk '$0 >= from' from=$(date -u -d -15min "+####<%d-%b-%Y %H:%M:%S o'clock GMT>") test.log
$from in awk code does not mean the value of from but the fromth field. This does not make any sense if field is not a number.
the -15min in your date call are part of the format specifier, not the date spec.
Either backticks or $(), not both at the same time. Unless you want to run the output of date as a command and use the output of that.
By the way, I suspect that the lexicographical comparison of date strings formatted this way will not yield the result you want. It will not result in later dates being considered greater than earlier ones in all cases -- particularly at the end/beginning of months. 28-Feb is lexicographically greater than 01-Mar.
Addendum: Ah, I figured this seemed familiar. I left a solution to your previous question that does not have this problem before it was closed as a duplicate of something that doesn't really work for this case.

Retrieving File name for bash/shell programing

I need to access two files in my shell script. The only issue is , I am not sure what the file name is going to be as it is system generated.A part of the file name is always constant , but the rest of it is system generated , hence may vary. I am not sure how to access these files?
Sample File Names
Type 1
MyFile1.yyyy-mm-dd_xx:yy:zz.log
In this case , I know MyFile1 portion is a constant for all the files, the other portion varies based on date and time. I can use date +%Y-%m-%d to get till MyFile1.yyyy-mm-dd_ but I am not sure how to select the correct file. Please note each day will have just one file of the kind. In unix the below command gives me the correct file .
unix> ls MyFile1.yyyy-mm-dd*
Type 2
MyFile2.yyyymmddxxyyxx.RandomText.SomeNumber.txt
In this file , as you can see Myfile2 portion is common,I can user Date +%Y%m%d to get till (current date) MyFile2.yyyymmdd, again not very clear how to go on from there .In unix the below command gives me the correct file .Also I need to have previous date in the dd column for File 2.
unix> ls MyFile2.yyyymmdd*
basically looking for the following line in my shell script
#!/bin/ksh
timeA=$(date +%Y-%m-%d)
timeB=$(date +%Y%m)
sysD=$(date +%d)
sysD=$((sysD-1))
filename1=($Home/folder/MyFile1.$timeA*)
filename2=($Home/folder/MyFile2.$timeB$sysD*)
Just not sure how to get the RHS for these two files.
The result when running the above scripts is as below
Script.ksh[8]: syntax error at line 8 : `(' unexpected
Perhaps this
$ file=(MyFile1.yyyy-mm-dd*)
$ echo $file
MyFile1.yyyy-mm-dd_xx:yy:zz.log
It should be noted that you must declare variables in this manner
foo=123
NOT
foo = 123
Notice carefully, bad
filename1=$($HOME/folder/MyFile1.$timeA*)
good
filename1=($HOME/folder/MyFile1.$timeA*)

KSH: How to add Date Time at the end of a New File e.g. FileMMDDYYYYHHMM

I am having a requirement where I want the output as
FileMMDDYYYYHHMM
I tried the following things
I am having a file called abc.ksh in which I have Declared a variable as
MYDATETIME=${DATE}${TIME}
and I am passing the parameter as
cp $MY_DATA_DIR/My_Reports $MY_DATA_DIR/My_Reports$MYDATETIME
In the above I am getting the OutPut as My_Reports without MYDATETIME
I also used the echo command to Print the out put
echo "my datetime" $MYDATETIME
I got the Output as
my datetime
It is not adding the expected date time to File [My_Reports].
Am I doing something wrong here?
Can anyone suggest or guide me on the same.
Like others have said, I don't see you initializing your DATETIME variable. If I was in your shoes, I'd do the following:
$MY_DATA_DIR=/this/is/my/derp
MYDATETIME=`date +%m%d%Y%H%M`
cp $MY_DATA_DIR/My_Reports $MY_DATA_DIR/My_Reports$MYDATETIME
If you want to split date and time, then:
$DATE=`date +%m%d%Y`
$TIME=`date +%H%M`
$MYDATETIME = ${DATE}${TIME}
Enjoy!

How can I append time to this command prompt date command

I am trying to run a .bat file with command prompt to add time to the date.
Currently, I have this code
MOVE...\folder\^"Mytest %DATE:/=-%.csv^"
This produces
..\folder\Mytest Thu 12-06-2012.csv
I want to get
..\folder\Mytest Thu 12-06-2012 21:45.csv
Tried all kinds of things but failed miserably. Help would be greatly appreciated.
This will work:
%date:/=-% %time:~0,5%.csv
The %time% uses the current time; the :~ means "a substring of", and the 0,5 says "starting at the first character (index 0) and continuing for 5 characters", so the entire thing means "give me the first 5 characters of the output of time".
Using this at a command prompt:
C:\>echo %date:/=-% %time:~0,5%
outputs
Thu 12-06-2012 18:19
The format you're using is going to cause problems with sorting, though. My advice would be to drop the day of the week portion, and change the date output to CCYY-MM-DD, which will be much more useful when you're trying to find a specific date. You can use this:
echo %date:~10,4%-%date:~4,2%-%date:~7,2% %time:~0,5%
which outputs
2012-12-06 18:33

Resources