0

I have a log file named version.txt and need to filter out all words with key word (time= and output should look like (time=451) etc.

Below are the sample entries from log file. Highlighted the entries which I want to list down. If I do cut or trim it prints the entire line instead of word (time=.

Please suggest a command to list down word from (time= including the number up to the closing ).

2020-03-10 06:48:20 [http-nio-7001-exec-7] INFO  [5e6770737be8a35b5fef38f7be2a2635] [5fef38f7be2a2635] [] c.l.e.i.a.c.ItemAvailabilityControllerImpl - DeliveryMethod(sosItmNbr=null, fullMtdTyp=3, fullMtdMsg=Delivery, fullCarrier=null, fullCarrierSvc=null, fullTransitMode=null, fullLctNbr=0, restMsg=null, isAvlSts=false, reqStates=[], onhandQty=0, totalQty=0, itmLdTmAvlQty=0, itmLdTm=null, itmConsolidationDate=null, itmLdTmDays=null, itmLdTmDaysLow=null, fullPath=null)])]) (time=451) 
2020-03-10 06:48:20 [http-nio-7001-exec-28] INFO  [5e677073e64bd99b5997b5bd20c3c4e0] [5997b5bd20c3c4e0] [] c.l.e.i.a.c.ItemAvailabilityControllerImpl - Finished availability process; Response: IAResponse(locationItemData=[ResponseItem(lctNbr=6877, itemNbr=10000070, modID=1500040, omniID=null, vbuNbr=14692, itmTypCode=3, reqQty=17, itemAvailList=[DeliveryMethod(sosItmNbr=null, fullMtdTyp=1, fullMtdMsg=Parcel, fullCarrier=null, fullCarrierSvc=null, fullTransitMode=null, fullLctNbr=0, restMsg=null, isAvlSts=false, reqStates=[], onhandQty=0, totalQty=0, itmLdTmAvlQty=0, itmLdTm=null, itmConsolidationDate=null,(time=455)
2020-03-10 06:48:20 [http-nio-7001-exec-46] INFO  [5e6770731c4e323f4cb875712bb0d8ee] [4cb875712bb0d8ee] [] c.l.e.i.a.c.ItemAvailabilityControllerImpl - Finised (time=492)

The output from this example input should look like

(time=451)
(time=455)
(time=492)
3
  • Please edit your question and show how the output should look like. With the example input, do you want 3 output lines (time=451), (time=455) and (time=492)?
    – Bodo
    Mar 10, 2020 at 13:07
  • Do you only want (time= or do you also want the digits that follow?
    – Jeff Schaller
    Mar 10, 2020 at 13:14
  • I edited the question to make it clear as I understand it based on the comments and the accepted answer. Please also add the new requirement with the expected output as requested in a comment to the accepted answer.
    – Bodo
    Mar 10, 2020 at 14:32

1 Answer 1

3

The question is not fully clear.

The command

grep -o '(time=[[:digit:]]*)' inputfile

with the example input from the question prints

(time=451)
(time=455)
(time=492)

Edit to cover additional requirement from a comment:

Adding the date and time field from the start of the lines to the output requires a different command.

If all lines of the input contain (time=...) and start with date and time fields separated by single space characters, you can use

sed 's/^\([-0-9]* [:0-9]* \).*\((time=[[:digit:]]*)\).*/\1\2/' inputfile

This prints

2020-03-10 06:48:20 (time=451)
2020-03-10 06:48:20 (time=455)
2020-03-10 06:48:20 (time=492)

If there are other non-matching lines you can combine this with the same grep pattern as above, but omit -o.

grep '(time=[[:digit:]]*)' inputfile | sed 's/^\([-0-9]* [:0-9]* \).*\((time=[[:digit:]]*)\).*/\1\2/'

Note that the search pattern in my sed command is not very strict.

The part [-0-9]* [:0-9]* matches any number of digits and dashes in any combination (the date) followed by a single space, any number of digits and colons (the time) followed again by a single space. The pattern is anchored to the start of the line by the preceding ^.

So it would also match wrong date and time fields like ---123--456- 9876:54321 or even two spaces at the start of the line.

8
  • Thanks it worked..Got what i need
    – Surendar
    Mar 10, 2020 at 13:18
  • @Surendar Accept the answer if it helped you :) Mar 10, 2020 at 13:21
  • Thanks for the command.. if i need the time of as well then please share the command... Output should be something like (time=451) 2020-03-10 06:48:20 (time=455) 2020-03-10 06:48:20 (time=492) 2020-03-10 06:48:20
    – Surendar
    Mar 10, 2020 at 13:38
  • @Surendar Please show the expected output in your question. Format it as a code block. You can do this by selecting the text using mouse or keyboard and using the {} tool of the editor field. Does the input contain only lines that match the sample lines from the question? If yes, please state this in the question. If not, show also other non-matching lines.
    – Bodo
    Mar 10, 2020 at 14:07
  • Thanks Bodo. I again got what i need...I used the second command which start with grep and it gave me the expected results.. Like below... 2020-03-10 06:48:20 (time=460) 2020-03-10 06:48:20 (time=451) 2020-03-10 06:48:20 (time=451) 2020-03-10 06:48:20 (time=451) 2020-03-10 06:48:20 (time=451) 2020-03-10 06:48:20 (time=451) 2020-03-10 06:48:20 (time=455) 2020-03-10 06:48:20 (time=469) 2020-03-10 06:48:20 (time=455) 2020-03-10 06:48:20 (time=457) 2020-03-10 06:48:20 (time=455) 2020-03-10 06:48:20 (time=451)
    – Surendar
    Mar 10, 2020 at 17:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .