Hello Ramneekgupta91,
Could you please try following(tested with GNU awk
).
awk --re-interval '{match($0,/[0-9]{4}-[0-2][0-9]-[0-9]{2} [0-2][0-9]:[0-5][0-9]:[0-5][0-9].*/);print substr($0,RSTART+24,RLENGTH-24)}' Input_file
OR
awk --re-interval '{match($0,/[0-9]{4}-[0-2][0-9]-[0-9]{2} [0-2][0-9]:[0-5][0-9]:[0-5][0-9],[0-9]{3} .*/);print substr($0,RSTART+24,RLENGTH-24)}' Input_file
Output will be as follows.
[ttp-/57.20.70.159:8111-35] ERROR com.lufthansa.lgt.exception.filter.LGTExceptionResolver - The error is : For input string: " S"
Adding more solution here.
awk --re-interval '{sub(/.*[0-9]{4}-[0-2][0-9]-[0-9]{2} [0-2][0-9]:[0-5][0-9]:[0-5][0-9],[0-9]{3} /,X,$0);print}' Input_file
EDIT: Above solutions may get month more than 12 and time more than 23 too(though one could trust that data will be in correct time format but for exact match and safer side), so edited regex above solutions as follows.
So let's say we have following Input_file:
/logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 [ttp-/57.20.70.159:8111-35] ERROR com.lufthansa.lgt.exception.filter.LGTExceptionResolver - The error is : For input string: " S"
/logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-18-24 19:12:48,602 [ttp-/57.20.70.159:8111-35] ERROR com.lufthansa.lgt.exception.filter.LGTExceptionResolver - The error is : For input string: " S$$$$$$$"
/logs/GRAS/LGT/applogs/lgt-2016-28-24/2016-08-24.8.log.zip:2016-28-24 59:12:48,602 [ttp-/57.20.70.159:8111-35] ERROR com.lufthansa.lgt.exception.filter.LGTExceptionResolver - The error is : For input string: " S"
/logs/GRAS/LGT/applogs/lgt-2016-29-24/2016-08-24.8.log.zip:2016-29-24 59:12:48,602 [ttp-/57.20.70.159:8111-35] ERROR com.lufthansa.lgt.exception.filter.LGTExceptionResolver - The error is : For input string: " S"
/logs/GRAS/LGT/applogs/lgt-2016-29-24/2016-08-24.8.log.zip:2016-12-24 59:12:48,602 [ttp-/57.20.70.159:8111-35] ERROR com.lufthansa.lgt.exception.filter.LGTExceptionResolver - The error is : For input string: " S"
/logs/GRAS/LGT/applogs/lgt-2016-29-24/2016-08-24.8.log.zip:2016-11-24 59:12:48,602 [ttp-/57.20.70.159:8111-35] ERROR com.lufthansa.lgt.exception.filter.LGTExceptionResolver - The error is : For input string: " S"
/logs/GRAS/LGT/applogs/lgt-2016-29-24/2016-08-24.8.log.zip:2016-13-24 59:12:48,602 [ttp-/57.20.70.159:8111-35] ERROR com.lufthansa.lgt.exception.filter.LGTExceptionResolver - The error is : For input string: " S"
/logs/GRAS/LGT/applogs/lgt-2016-29-24/2016-08-24.8.log.zip:2016-13-24 79:12:48,602 [ttp-/57.20.70.159:8111-35] ERROR com.lufthansa.lgt.exception.filter.LGTExceptionResolver - The error is : For input string: " S"
I had put some more scenarios in it by adding 29th month in dates and having more than 59 mins of time too. So following code may resolve those things.
awk --re-interval '{match($0,/[0-9]{4}-(0[1-9]||1[0-2])-(0[1-9]||1[0-9]||2[0-9]||3[0-1]) ([0-1][1-9]||[0-2][0-3]):[0-5][0-9]:[0-5][0-9],[0-9]{3} .*/);if(substr($0,RSTART+24,RLENGTH-24)){print substr($0,RSTART+24,RLENGTH-24)}}' Input_file
Output will be as follows.
[ttp-/57.20.70.159:8111-35] ERROR com.lufthansa.lgt.exception.filter.LGTExceptionResolver - The error is : For input string: " S"
Because only 1st line meets the criteria so it is printing only that one.
Adding a non-one liner form of solution too now.
awk --re-interval '{
match($0,/[0-9]{4}-(0[1-9]||1[0-2])-(0[1-9]||1[0-9]||2[0-9]||3[0-1]) ([0-1][1-9]||[0-2][0-3]):[0-5][0-9]:[0-5][0-9],[0-9]{3} .*/);
if(substr($0,RSTART+24,RLENGTH-24)){
print substr($0,RSTART+24,RLENGTH-24)
}
}
' Input_file
Thanks,
R. Singh