Returning a specific line of text from files

First Objective :My objective is to parse a file (“Status.json”), search for a specific line of text (“last_communicated_timestamp”) and return that string of text , “last_communicated_timestamp”: “2019-11-04T08:48:53-08:00”, I’ve read through most of the posts and tried massaging similar scripts but can’t get it right to obtain the desired result.This is a sample of what I’ve tried so far and many iterations (of it) since:

following texts of firsts “last_communicated_timestamp” of lines whose (it starts with “last_communicated_timestamp”) of file “C:\Users\Administrator\Documents\Brown\Status.json”

I am dealing with the line starts with Some thing like lines starting with “ and contains “last_communicated_timestamp” so i was looking at how to modify above using


exists line whose (it starts with “#” and it contains “”) of file “c:\foo.txt”

here is a copy and paste of file in the attachment

{
“snapshot_time”: “2019-11-04T08:49:29-08:00”,
“ProductInfo”: {
“version”: “2.0.1540.8”,
“last_communicated_timestamp”: “2019-11-04T08:48:53-08:00”,
“serial_number”: “0a4cdfdf-b4b0-489f-9cc0-c544ead85fe9”,
“device_name”: “ADMINIB-7N4T179”
},
“Policy”: {

Hello jwgibson949,

This might be what you are looking for:

q: lines whose (it contains "last_communicated_timestamp") of file "c:\test.json"
A: "last_communicated_timestamp": "2019-11-04T08:48:53-08:00",

Regards,
Vitaliy

1 Like

Might I recommend the use of the built-in JSON inspectors?

q: values of keys "last_communicated_timestamp" of values of keys "ProductInfo" of jsons of files "c:\temp\test.json"
A: 2019-11-04T08:48:53-08:00

One stumbling point for me was trying to get out of the habit of “values of keys of keys” the way we would traverse the Regstry; for JSON it’s “values of keys of values of keys” instead.

2 Likes

Thanks for the quick replies and suggestions as both worked . The next objective is below and seems to be a bit more complicated.

Here is the 2nd objective I have.

My objective is to parse a file (“master.log”), search for a the last line of the file that has specific line of text (xsrpc connection is active) and return the preceding string of text which is the timestamp”:0927/163753z” that proceeds the text. Here is the entire line with the timestamp in bold. The log file is continuous and I will need to run this process once a day and capture the last line of the file that meets below. I can show entire line or just the time stamp either way.

[0927/155747z:INFO:xsrpc_controller.cc(178)] xsrpc connection is active

The lines ends with a LF

I’ve read through most of the posts and tried massaging similar scripts but can’t get it right to obtain the desired result.

I’d start with the ‘line number’ inspector.

To return line 50 of a file, it’s simply line 50 of file "c:\temp\foobar.txt".

You can combine that with the ‘maxima’ operator to get the largest line number of a line matching a condition.

So the start of you query might be

lines (maxima of line numbers of lines containing "xsrpc connection is active" of it) of file "c:\temp\foobar.txt"

1 Like

Wow Jason, that worked really well. Thanks a bunch.

1 Like

Jason is it possible to only capture the time stamp ,highlighted in Bold, data instead of the entire line ?

[0927/155747z:INFO:xsrpc_controller.cc(178)] xsrpc connection is active

Sure, it’s just text parsing at that point.

[0927/155747z:INFO:xsrpc_controller

Try combining this with the lines query from earlier…

preceding texts of firsts ":" of following texts of firsts "[" of lines (...)

Thanks Jason that worked and i understand how you parsed the text. Thanks again for showing me so much.

1 Like

Just word of caution, when looking into parsing files. You might want to put the parsing to evaluate only IF the file is of certain size, not blindly. lines of is going to parse the entire file line by line to get you the lines you are interested and if the file is let’s say 5-10-50-100mbs in size it really can cause significant performance issues.

Thanks for that ageorgiev.