how to get the next hour's date string in bash - bash

I 'm using bash
I have a date string now, for example:
2015111301
(yyyymmddHH)
how to get the date string of next hour?
that is:
2015111302

Try this:
in="2015111301"
out="$(date -d "${in:0:8} ${in:8:2}:00:00 +1hour" '+%Y%m%d%H')"
echo "$out"
Output:
2015111302
See: 3.5.3 Shell Parameter Expansion

This below script worked for me
#!/bin/bash
tempval=$(echo 2015111301 | sed 's/\(.\{8\}\)/\1 /g')
tempval=$(echo "$tempval 1 hour")
date -d "$tempval" +%Y%m%d%H

Related

sed extract data only between time range

I have a file with the following entries:
MySql-DataBase-2020-09-22_183748.zip
MySql-DataBase-2020-09-22_184023.zip
MySql-DataBase-2020-09-23_205331.zip
MySql-DataBase-2020-09-23_205606.zip
MySql-DataBase-2020-09-24_200123.zip
MySql-DataBase-2020-09-24_200358.zip
MySql-DataBase-2020-09-25_115839.zip
MySql-DataBase-2020-09-25_120115.zip
MySql-DataBase-2020-09-26_094608.zip
MySql-DataBase-2020-09-26_094843.zip
MySql-DataBase-2020-09-27_122523.zip
MySql-DataBase-2020-09-27_122758.zip
MySql-DataBase-2020-10-01_230024.zip
MySql-DataBase-2020-10-01_230300.zip
MySql-DataBase-2020-10-02_120944.zip
MySql-DataBase-2020-10-02_121219.zip
MySql-DataBase-2020-10-03_151414.zip
MySql-DataBase-2020-10-03_151649.zip
MySql-DataBase-2020-10-04_211059.zip
MySql-DataBase-2020-10-04_211334.zip
MySql-DataBase-2020-10-05_064049.zip
MySql-DataBase-2020-10-05_064324.zip
I want to extract Files which are between today's date & 3 days back.
For today's date 2020/10/05, 3 days back is 2020/10/02.
The output must be:
MySql-DataBase-2020-10-02_120944.zip
MySql-DataBase-2020-10-02_121219.zip
MySql-DataBase-2020-10-03_151414.zip
MySql-DataBase-2020-10-03_151649.zip
MySql-DataBase-2020-10-04_211059.zip
MySql-DataBase-2020-10-04_211334.zip
MySql-DataBase-2020-10-05_064049.zip
I tried using this command to gets 3days back date
date --date='-3 day' '+%Y/%m/%d'
And then used these command to get output between date range
sed -n '/3day=date --date='-3 day' '+%Y/%m/%d'/,/date/p' s.txt
I am getting this error:
sed: -e expression #1, char 20: unterminated address regex
Please Help me to fix this issue. I'll be using this in a bash script.
Using a process substitution to generate the dates and feed that to grep as the patterns to search for:
grep -F -f <(for d in {0..3}; do date -d "$d days ago" "+%F"; done) file
sed -n "/$(date --date='-3 day' '+%Y-%m-%d')/,/$(date +'%y-%m-%d')/p"
MySql-DataBase-2020-10-02_120944.zip
MySql-DataBase-2020-10-02_121219.zip
MySql-DataBase-2020-10-03_151414.zip
MySql-DataBase-2020-10-03_151649.zip
MySql-DataBase-2020-10-04_211059.zip
MySql-DataBase-2020-10-04_211334.zip
MySql-DataBase-2020-10-05_064049.zip
Notice, the use of double quotes at the outermost level. Also, notice the same format of date in both boundaries.

Shell script to fetch log (json format) file between date and timestamp

The log file folder structure is \Mainfolder\folder1\year(2020)\month(07)\date(24)*.json.
Ex: \Mainfolder\folder1\2020\07\24\filename.json.
The .json file is getting created every hour, like 00:00:00_00:59:59.json, 01:00:00_01:59:59.json and so on.
I have to search under the .json file with following inputs.
My current inputs are keyword, start date. Currently I'm taking that Date, and keyword and able to get the output in a file.
Current script for your reference:
#!/bin/bash
set +x
DTE=$(date "+%d-%m-%Y-v%H%m%s")
Date=$1 #yyyy/mm/dd
Keyword=$2 #keyword in string
Start_Time=$3 #hh:mm
End_Time=$4 #hh:mm
BKT=bucketpath/mainfolder/
output=$(gsutil cat -h gs://bucketpath/mainfolder/"$Date"/* | egrep "$Keyword")
echo $output >> $"/tmp/folder/logoutput-$DTE"
gsutil cp -r /tmp/folder/logoutput-$DTE gs://bucketpath/mainfolder/
I have to add end date, Start_Time & End_Time and search in the .json file and get the output in a file like above.
I tried to use awk & sed, but i'm unable to get the output.
Could anyone help me on this script please.
Thanks in advance.
I prepared following script to collect the logs between date and timestamp along with keyword. My log file is in .json format.
The reason for posting here is, it might help someone who is looking for similar script.
#!/bin/bash
set +x
DTE=$(date "+%d-%m-%Y-v%H%m%s")
startdate=$1
enddate=$2
start_Time=$3
end_Time=$4
keyword=$5
BKT=storage/folder
i=$start_time
i1=$(sed 's/.\{3\}$//' <<< "$i")
j=$end_time
j1=$(sed 's/.\{3\}$//' <<< "$j")
curr="$startdate"
while true; do
echo "$curr"
[ "$curr" \< "$enddate" ] || break
output=$(gsutil cat -h gs://storage/folder/"$curr"/"$i1:00:00_$j1:59:59*" | sed -n '/"timestamp":"[^"]*T'$i':/,/"timestamp":"[^"]*T'$j':/p' | grep "$keyword")
echo $output >> $"/tmp/folder/mylog-$DTE"
curr=$( date +%Y/%m/%d --date "$curr +1 day" )
done
gsutil cp -r /tmp/folder/mylog-$DTE gs://storage/folder/

Covert string to a date bash

I am trying to convert ProcessedTime": 1545166011.794351 to a date using date function in bash script, what is the best way to convert it ?
date -d #154166011.794351
Date command should work fine.
With GNU awk:
echo 154166011.794351 | awk '{print strftime("%Y-%m-%d %H:%M:%S",$0)}'
Output:
1974-11-20 08:53:31
Source: https://unix.stackexchange.com/a/168317/74329

output a file with a variable name in shell

So I am trying to output a file with the name of like: lastlogin-"yyyymmdd" where it contains the current date.
I figured out the date should be : date +"%Y%m%d" and I tried to do a variable
now = date +"lastlogin-%Y%m%d.txt"
filename = ${now}
xxxxx > ${filename}
but nothing seems to work
Please help
Use command substitution:
lastlogin-"$(date '+%Y%m%d')".txt
To save in a variable:
filename="lastlogin-"$(date '+%Y%m%d')".txt"
and then do:
echo 'foobar' >"$filename"
You should use $() for command execution and storage of result:
now=$(date +"lastlogin-%Y%m%d.txt")

Grepping a specific string from a file in script

I have following file:(A sample file with filename: 2015_09_22_processedPartnumList.txt, Location: /a/b/c/itemreport)
DataLoader_trace_2015_09_22_02_01_32.0956.log:INFO: 2015-09-22
Data Processing Starts : 12345678
I just want to get all the ids from the above file i.e. 12345678 .... (each id in a separate line, not comma separated) in a file /a/b/c/d/ids_date +%d_%m_%Y_%H_%M_%S.log
I have written the following script, but the file I am getting is empty. Without showing any exception or anything. So, it is very difficult for me to identify the errors. Please tell me what is wrong in the script.
LOGDIR=/a/b/logdir
tr=`date +%p`
echo $tr
if [ $tr = "PM" ];
then
date=`date +%Y-%m-%d`
echo "considering today's date for grepping logs"
else
date=`date -d '1 day ago' +%Y-%m-%d`
echo "considering yesterday's date for grepping logs as job run is delayed"
fi
ITEM_FILE=/a/b/c/d/ids_`date +%d_%m_%Y_%H_%M_%S`.log
After implementing grep in PCRE, I am getting this and not any ids are being copied into the new file.
If your grep supports PCRE, you can do:
grep -Po '.*:\s\K\d+$' /a/b/c/itemreport/2015_09_22_processedPartnumList.txt \
>/apps/feeds/out/catalog/ItemPartnumbers_"$(date '+%d_%m_%Y_%H_%M_%S')".log
.*:\s will match upto the space after :, \K will discard the match
\d+$ will match our desired portion i.e. the digits till the end of the line
Example:
% grep -Po '.*:\s\K\d+$' 2015_09_22_processedPartnumList.txt \
>ItemPartnumbers_"$(date '+%d_%m_%Y_%H_%M_%S')".log
% cat ItemPartnumbers_09_11_2015_11_30_49.log
13982787
14011550
13984790
13984791
14176509
14902623
14924193
14924194
13982787
46795670
46795671
That's not very good solution, but it's working.
cat your\ file | cut -d ':' -f2-2 | tr -d INFO

Resources