WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Script for parsing simple text based db files.  (Read 2148 times)

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11675
Script for parsing simple text based db files.
« on: June 26, 2022, 12:14:24 AM »
The attached script searches a  db  file and returns only records that contain all of the terms being
searched for, I.e. records that contain term1 AND term2 AND term3 AND ... etc.

It was originally written to parse the result of  udevadm monitor --property > filename.txt  for specific
devices, but can be used for any file using the same format.

Some examples of simple text based db files include  /proc/bus/input/devices , /proc/crypto , provides.db , and
the result of  udevadm monitor --property > filename.txt.

A record consists of a series of consecutive lines of text known as fields.
The end of a record is marked with a blank line containing only a newline (\n) character.

To see the usage message:
Code: [Select]
$ ./RecordScan.sh -h
The forum throws an error when trying to post the entire usage message, so
here's the syntax and brief explanation:
Code: [Select]
  Usage:

    ./RecordScan.sh filename.txt text 'more text' text 'text=this:that'

    The first argument must be the file to parse. The remaining arguments
    are the text to search for. Text containing spaces or punctuation
    should be enclosed in single quotes

The actual search is performed by an  awk  command and it's really fast.
Searches including a forward slash need to be escaped or  awk  will report an error:
Code: [Select]
tc@E310:~/Scripting/RecordScan$ time ./RecordScan.sh /mnt/sda1/tce/provides.db 'bin\/grep'

/mnt/sda1/tce/provides.db contains 2682 records.
Searching /mnt/sda1/tce/provides.db for bin\/grep
3 records matched search. Results saved in ./RecordScan.txt

real    0m 0.58s
user    0m 0.22s
sys     0m 0.13s
tc@E310:~/Scripting/RecordScan$
Without  awk , the previous version of this script took about 12 seconds to complete.

After downloading the script, make it executable:
Code: [Select]
chmod 775 /RecordScan.sh