if the string and pattern itself stays the same and the date string part stays at N digits long then you could do something like this first.
remove the RED part.
Code:
userx%slackwhere ⚡ ~ ⚡> echo $string
7/13/17 02:10:09.318 PM BST [INFO] [TimPollThreadPool.Thread1] [Manager.com.timestock.tess.services.tim.TimIo] File 'Wynyard_MTP_Primary-defect-14999514040000899961.xml' read, length=20417
#chop off left end
userx%slackwhere ⚡ ~ ⚡> newL=${string##*defect-}
#chop off right end to period
userx%slackwhere ⚡ ~ ⚡> newR=${newL%%.*}
#chop down the number to get date
userx%slackwhere ⚡ ~ ⚡> Date=${newR:0:10}
userx%slackwhere ⚡ ~ ⚡> echo $Date
1499951404
I do not see how you're getting that string off the file in this block of code but, I'll let you work that out?
Just add what I did to your code: (if you want to)
Code:
CURRENTDATE=$(date +%s)
#get last date or Other Date
newL=${string##*defect-}
newR=${newL%%.*}
Date=${newR:0:10}
OTHERDATE="$Date";
DIFFDATE=$((CURRENTDATE-OTHERDATE));
echo -n "Seconds: "$DIFFDATE" - "
#date -d @$DIFFDATE +%Y%m%d-%H:%M:%S
date -d @$DIFFDATE +%c
exit 0
so now it does not matter if that date changes as long as the string has the key 'pattern'
defect- before the date needed and that date keeps the same amount of digits - it will get whatever it is so you can do the math on it.
this worked too using
egrep
Line 1:
Code:
7/13/17 02:10:09.318 PM BST [INFO] [TimPollThreadPool.Thread1] [Manager.com.timestock.tess.services.tim.TimIo] File 'Wynyard_MTP_Primary-defect-14999514040000899961.xml' read, length=20417
userx%slackwhere ⚡ ~ ⚡> gerped=$( echo $string | egrep -o [0-9]{12})
userx%slackwhere ⚡ ~ ⚡> echo $gerped
149995140400
but too many 0 zeros?
changing t to 10 got me this
Line 2
Code:
userx%slackwhere ⚡ ~ ⚡> gerped=$( echo $string | egrep -o [0-9]{10})
userx%slackwhere ⚡ ~ ⚡> echo $gerped
1499951404 0000899961
so if using egrep with {12} just chop off the zeros on the right side before using it.
with line 1 do this
Code:
userx%slackwhere ⚡ ~ ⚡> NewGerp=${gerped%*00}
userx%slackwhere ⚡ ~ ⚡> echo "$NewGerp"
1499951404
with line 2: do this,
Code:
userx%slackwhere ⚡ ~ ⚡> gerped=$( echo $string | egrep -o [0-9]{10})
userx%slackwhere ⚡ ~ ⚡> echo $gerped
1499951404 0000899961
userx%slackwhere ⚡ ~ ⚡> NewGerp=${gerped%%* }
userx%slackwhere ⚡ ~ ⚡> echo "$NewGerp"
1499951404
0000899961
well that is just weird. has to be a new line hidden in there?
Fixed it:
Code:
7/13/17 02:10:09.318 PM BST [INFO] [TimPollThreadPool.Thread1] [Manager.com.timestock.tess.services.tim.TimIo] File 'Wynyard_MTP_Primary-defect-14999514040000899961.xml' read, length=20417
#is suppose to get first 10 digits but splits them
userx%slackwhere ⚡ ~ ⚡> NewDate=$(echo $string | egrep -o [0-9]{10})
userx%slackwhere ⚡ ~ ⚡> echo $NewDate
1499951404 0000899961
#just strip it again
userx%slackwhere ⚡ ~ ⚡> NewNewGerp=${gerped:0:11}
userx%slackwhere ⚡ ~ ⚡> echo $NewNewGerp
1499951404
used 11 to be sure to get the space out of there as well.
http://tldp.org/LDP/abs/html/string-manipulation.html