Link to home
Start Free TrialLog in
Avatar of sherlock1
sherlock1

asked on

In bash search text in a file on a specific date range & output results

machineinfo.txtHi Experts,

In Bash unix shell I would like to search text in a file named “machineinfo.txt” (contains information about software installed on a computer) and search/filter on an install date = “” line between a specific date range from 20190401 to 20190513 and output the following lines in date order (most recent date at top of list) from the results of the search based on the date range (below is example output)

DisplayName = "Symantec Endpoint Protection
InstallLocation = "C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\14.2.1031.0100.105\"
DisplayVersion = "14.2.1031.0100"
InstallDate = "2019022

I’m not sure what command needs to be used, any help appreciated please? Attached is an example of the machineinfo.txt file. I’ve kept the file small with just a few software entries.

Thanksmachineinfo.txtmachineinfo.txt
Avatar of David Favor
David Favor
Flag of United States of America image

I've been thinking about this for a bit now considering how to do this in an efficient way.

Using bash scripting... I suppose you could... if you have unlimited time + budget...

Or you can use PERL + be done in a few minutes.

Consider this script, which is one implementation of paragrep (paragraph based grep tool) which outputs paragraphs on any match in the paragraph.

Note: This is a hastily constructed bit of code is likely buggy.

I've attached the paragrep script.

Usage example...

paragrep "installdate.+201[89]" machineinfo.txt

Open in new window


Scoping of data returned is to look at installdate along with some regular expression to correctly express the date range desired.

So "installdate.+201[89]" means look for installdate in a paragraph followed by any characters (.+) for any 2018-2019 dates (201[89]).

For finer grain date matching, use grep-ish syntax, so 20182[23] will return Feb + March 2018 dates, etc...

This allows for files to be scanned only once, rather than multiple times... in case you have big files or complex date ranges to scan.

Note: Exercise of converting paragrep from PERL to BASH is left to you. Certainly be a good learning experience.
paragrep.pl
Avatar of sherlock1
sherlock1

ASKER

Thanks for your reply David. Ive never used paragrep / perl but sounds like that a tool worth exploring so ill look into it. Im running bash on a Windows 10 client computer and ideally was hoping to not need to use another tool instead of bash. i guess paragrep is based on perl

Sounds like what im trying to do may be more tricky than what i imagined
You're welcome!

Hope the script helps you!

Yep, I provide a PERL script because most machines have PERL.

As I said, likely this can be done in BASH + rather than a few minutes to produce a PERL script... I'd have had to work days to get a BASH script working to implement the same logic.

Call me lazy or efficient, PERL seemed a better fit for this particular task.
this will display the Displayname and installtime sorted as required

sed -ne '/DisplayName/ h ; /InstallDate = "20190\(40[1-9]\|50\|51[123]\)/ { G ; s/\n/ /g ; p }' /path/to/machineinfo.txt | sort -k3

you obviously can change the format as you see fit

--

side notes :
- awk or perl should allow better date handling than messing with eregs
- the windows os probably provides other means to retrieve that same information
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.