I am trying to grep specific timestamp + required string inside logs, please see below code
month=`echo $(date) | cut -d ' ' -f 2`
zero=0
year=`echo $(date) | cut -d ' ' -f 6`
date1=$zero$(echo $(date) | cut -d ' ' -f 3)
currenttiemstamp=`echo $(date) | cut -d ' ' -f 4`
currenthr=`echo $(date) | cut -d ' ' -f 4 |cut -d ':' -f 1`
currentmin=`echo $(date) | cut -d ' ' -f 4 |cut -d ' ' -f 2`
currentmin1=`expr $(echo $(date) | cut -d ' ' -f 4 |cut -d ':' -f 2) - 6`
x="Error Message"
**Assigning timestamp string to a variable
stringtocheck=$date1" "$month" "$year" "$currenthr":"$currentmin1**
#printing timestamp string
echo "$stringtocheck"
cd /log/
**cat xyz.log |grep "^ "$stringtocheck""|grep "^ "$x""**
even in above cat command I tried with single quote it is not showing the result anyway
Please let me now if I am doing right thing here , I am completely new in shell scripting world.
datecommand can show you everything you're asking for without piping through cut. I tried your first variable and got today's date, not month.echo $(date).datea lot to get different fields; why not just run it once with a format string to get the fields you want (I think you wantdate +"%d %a %Y %H"to get everything exceptcurrentmin1)? Andcurrentmin1is weird; if the current minute is less than 6, it'll be negative; if between 6 and 15, it'll be a single digit. Is this intentional? If not, what is the intent?grep '^thing' | grep '^somethingelse'will never print anything.