I want to extract date and time from below sequence

/hs_nfs/hsfiq/AXZIP-PP567/was_logs/was_admin_logs/start_WPS.log.04-24-2018-08.36.Apr24.zip:ADMU3000I: Server Node_axzip-pp567_AppClusterMember open for e-business;

I want to extract fields as

Date          Time
04-24-2018   08.36
  • Please edit your question and tell us what parts of the name we can rely on. I assume you need toi be able to do this for different values, so what will be constant? Will the date always be after the exact string start_WPS.log.? Will the date always be characters 65-74 and the time always be characters 76-80? We can't help you parse a string if you don't explain what parts of the string are variable. – terdon Apr 25 '18 at 19:14
  • HI terdon , please see below I tried command zgrep 'open for' $path/start_WPS.log.*"$yesterdayDate"*|cut -f3,4 -d '.' O/p but I dont want - after date 04-24-2018-08.36 04-24-2018-22.06 – khuharshree Apr 25 '18 at 19:19
  • As said before, please edit your question and answer the questions I asked. – terdon Apr 25 '18 at 19:21
  • yes date will always come after start_WPS.log and with given set of characters for date and time – khuharshree Apr 25 '18 at 19:27
  • @khuharshree Please don't clarify your question in comments. Edit the text of the question instead. – Kusalananda Apr 25 '18 at 19:56
sed -E -e 's/^.*log\.//' -e 's/\.[A-Z].*//' -e 's/([0-9]{4})-/\1 /'

The first expression will delete everything in the string up to log. just before the date.

The second expression will delete everything from the dot following the time (the only dot that is followed by an upper-case letter).

The third expression will replace the dash after a four-digit number (the year) with a space.

Given the input in the question, this would produce

04-24-2018 08.36
  • getting below error bash-4.2$ zgrep 'open for' $path/start_WPS.log.*"$yesterdayDate"*|sed -E -e 's/^.*log\.//' -e 's/\.[A-Z].*//' -e 's/([0-9]{4})-/\1 /' sed: Not a recognized flag: E – khuharshree Apr 25 '18 at 19:47
  • I use below command first to list those file contains specific pattern for e.g "open for" and then apply cut operation to extract DATE & TIME bash-4.2$ zgrep 'open for' $path/start_WPS.log.*"$yesterdayDate"* /pm_nfs/pmfin/AXKIN-PPT4VOW4/was_logs/was_admin_logs/start_WPS.log.04-24-2018-08.36.Apr24.zip:ADMU3000I: Server Node_axkin-ppt4vow4_AppClusterMember open for e-business; – khuharshree Apr 25 '18 at 19:50
  • @khuharshree You may be using an old version of sed. Try replacing -E with -r. – Kusalananda Apr 25 '18 at 19:54
  • @khuharshree I'm not going to read any of that. Don't post long pieces of code in comments. If you want to clarify something, do that by editing your question. – Kusalananda Apr 25 '18 at 19:55

If date & time have a unique format in each line that looks like NN-NN-NNNN-NN.NN where N is a number , then you can use a single grep:

$ grep -Eo '[0-9]{2}-[0-9]{2}-[0-9]{4}-[0-9]{2}[.][0-9]{2}' file1
#to remove the third (last) dash you can pipe to sed
$ grep -Eo '[0-9]{2}-[0-9]{2}-[0-9]{4}-[0-9]{2}[.][0-9]{2}' file1 |sed 's/-/ /3g'
04-24-2018 08.36

If you want to also 'bind' above regex after WPS.log. you can use gnu grep with -P switch (perl regex support):

$ grep -Po '^.*WPS.log.\K[0-9]{2}-[0-9]{2}-[0-9]{4}-[0-9]{2}[.][0-9]{2}' file1 |sed 's/-/ /3g'
04-24-2018 08.36
#\K == forget everything captured so far == forget ^.*WPS.log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.