Andrea
Andrea

Reputation: 67

Bash script to split log file every string occurrence, and report inside the file specified line

I have a log file,to be precise it's custom application report similar to a logfile. I would like to create a bash script to manage this log, and generate from this single file, "n" numbers of file when this script is launched. The goal is to "split" this logfile on "string" occurrences, and catch the word after this string, and ignoring all the rest in the line, to use this word as part of filename.

I see some base script to split a logfile every word or string occurrences, but it's not making any chance to save or manipulate the "content" in some other method, it's just a "splitter" and save with filename 1.txt,2.txt and so on.

To have a context, we're talking about a report generated to "count" the licenses of a software,the report is something like:

blabla - Copyright (c) 1989-2016 Some Cool software without decent report. All Rights Reserved.
Cool Software License status on Wed 10/12/2016 03:02

License server status: port@serverhostname
    License file(s) on serverhostname: C:\Path\To\File\lic.txt:

  serverhostname: license server v0.7b

Users of service1:  (Total of 182 licenses issued;  Total of 0 licenses in use)

Users of service2:  (Total of 182 licenses issued;  Total of 180 licenses in use)

  "service2" v61.8, vendor: VENDORNAME
  floating license

    USER123 PC1234 PC1234 (v61.7) (SERVER/2501 4605), start Wed 10/12 14:56, 10 licenses
    USER311 PC3331 PC3331 (v61.2) (SERVER/2501 10621), start Mon 10/10 17:55, 12 licenses

Users of service3:  (Total of 182 licenses issued;  Total of 0 licenses in use)

Users of another_services:  (Total of 182 licenses issued;  Total of 0 licenses in use)

  "another_services" v61.8, vendor: ABAQUSLM
  floating license

    USER0021 PCNAME1 PCNAME1 (v61.4) (SERVER/2501 6852), start Wed 10/12 14:44
    USER0034 PCNAME03 PCNAME03 (v61.1) (SERVER/2501 13016), start Wed 10/12 10:51
    USER0123 PCNAME5 PCNAME4 (v61.4) (SERVER/2501 6852), start Wed 10/12 14:44

Users of plugin01:  (Total of 30 licenses issued;  Total of 0 licenses in use)

What I would like to do is create a separate text file for each service/plugin, so I need to "split", search/recognize the string, or better the line starting with:

Users of

followed by the service_name, that I need to store in a variable to use as part of filename.

Into the text file I would like to report only the string containing the USER and PC information, skipping the two description line where is repeated the name of the service, and some other info about the service.

The file is a dynamic report, so I can't really know before execute the script, the number or service, or the number of users under the line "Users of". Also, some service could not contain any line. If a service doesn't have any line, It's ok generate the text file without any line inside.

Any suggestion it's much appreciated because I'm really new to shell scripting.

Cheers!

Upvotes: 1

Views: 306

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185161

Try this with awk :

% awk '/^Users of/{name=$3} name{print > name}' file
% ls -1 *service*

Not all of your requirements are done, but feel free to improve it, I have done the bigger part

Upvotes: 1

Related Questions