Extracting information from Config files /text processing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting information from Config files /text processing
# 1  
Old 01-20-2009
Question Extracting information from Config files /text processing

Hello All,

This is my first post on this forums, which I consider one of the best of its kind. The reason for my post is that I want to export some information form Nagios configuration files to a DB. I know that there are other tools available to do this, like NDO, monarch, etc... But I want to do it my self as I have different requests and I will like to improve my scripting capabilities Smilie

This is an example from hosts.cfg file:

Code:
# 'SACVMW02' host definition
define host{
        use                     generic-host            ; Name of host template to use
        host_name               SACVMW02
        alias                   ESX SERVER
        address                 10.10.10.110
        check_command           check-host-alive
        contact_groups          NETWORKSGDL,CSG,DBAS,OPSGSSC
        notification_interval   30
        notification_period     24x7
        notification_options    d,r
        }

# 'CALUX901' host definition
define host{
        use                     generic-host            ; Name of host template to use
        host_name               CALUX901
        alias                   Calgary Unix Servers
        address                 10.120.240.16
        check_command           check-host-alive
        contact_groups          MONITORING,CAL-UNIX-ADMINS,NETWORKSGDL
        notification_interval   60
        notification_period     24x7
        notification_options    d,r
        }

I will like to know if there is a way to use a for loop that reads the text inside the { }'s, to define the first column inside it as a variable (or array in case of contact_groups, which has many comma separated values) and second column as value. It will also have to discard comments (# and ; lines)

Any suggestion is welcome, I already did some scripts that do the job using grep, sed and awk; reading line by line, but I think that there are other easier approaches that might improve the script, like reading text blocks instead of lines....

Thank you all!
Carlos
# 2  
Old 01-21-2009
hope below perl can help a little.

Just put them in 8 different array.

Code:
#!/usr/bin/perl
$n=0;
open FH,"<a.txt";
while(<FH>){
	next if /(\{|#)/;
  $n++ if /\}/;
	my @tmp=split(/  */,$_,3);
	$tmp[2]=~s/;.*//;
	$user[$n]=$tmp[2] if $tmp[1] eq "use";
	$host_name[$n]=$tmp[2] if $tmp[1] eq "host_name";
	$alias[$n]=$tmp[2] if $tmp[1] eq "alias";
	$address[$n]=$tmp[2] if $tmp[1] eq "address";
	$check_command[$n]=$tmp[2] if $tmp[1] eq "check_command";
	$contact_groups[$n]=$tmp[2] if $tmp[1] eq "contact_groups";
	$notification_interval[$n]=$tmp[2] if $tmp[1] eq "notification_interval";
	$notification_period[$n]=$tmp[2] if $tmp[1] eq "notification_period";
	$notification_options[$n]=$tmp[2] if $tmp[1] eq "notification_options";
}

# 3  
Old 01-21-2009
Quote:
Originally Posted by summer_cherry
hope below perl can help a little.

Just put them in 8 different array.

Code:
#!/usr/bin/perl
$n=0;
open FH,"<a.txt";
while(<FH>){
	next if /(\{|#)/;
  $n++ if /\}/;
	my @tmp=split(/  */,$_,3);
	$tmp[2]=~s/;.*//;
	$user[$n]=$tmp[2] if $tmp[1] eq "use";
	$host_name[$n]=$tmp[2] if $tmp[1] eq "host_name";
	$alias[$n]=$tmp[2] if $tmp[1] eq "alias";
	$address[$n]=$tmp[2] if $tmp[1] eq "address";
	$check_command[$n]=$tmp[2] if $tmp[1] eq "check_command";
	$contact_groups[$n]=$tmp[2] if $tmp[1] eq "contact_groups";
	$notification_interval[$n]=$tmp[2] if $tmp[1] eq "notification_interval";
	$notification_period[$n]=$tmp[2] if $tmp[1] eq "notification_period";
	$notification_options[$n]=$tmp[2] if $tmp[1] eq "notification_options";
}

Great! it works like a charm.

Another quick question Summer_cherry, how can I do a multi-dimensional array for Contact Groups, Notifications periods and notification options? As you can see in the example, those have different options separated by comma. Is there an easy way to extract those ?

Again, thank you for your support, your script was very helpful and by far better than my existing solution Smilie

Regards,
Carlos
# 4  
Old 01-21-2009
Smilie I answer to my self, I used split()

Thank you!
Carlos
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

2. Shell Programming and Scripting

extracting information from multiple files

Hello there, I am trying to extract (string) information ( a list words) from 4 files and then put the results into 1 file. Currently I am doing this using grep -f list.txt file1 . and repeat the process for the other 3 files. The reasons i am doing that (a) I do know how to code (b) each file... (4 Replies)
Discussion started by: houkto
4 Replies

3. Programming

extracting text files

i m unable to extract data from one text files to different text files..i am able to concat two text files in d same file ---------- Post updated at 03:21 PM ---------- Previous update was at 03:16 PM ---------- i want a c program for it (2 Replies)
Discussion started by: asd123
2 Replies

4. Shell Programming and Scripting

Extracting/condensing text from multiple files to multiples files

Hi Everyone, I'm really new to all this so I'm really hoping someone can help. I have a directory with ~1000 lists from which I want to extract lines from and write to new files. For simplicity lets say they are shopping lists and I want to write out the lines corresponding to apples to a new... (2 Replies)
Discussion started by: born2phase
2 Replies

5. Shell Programming and Scripting

matching and extracting info from text files

Hi all, I have two .txt file i.e. First text file: 2 4 1 4 Second text file 2 1.nii.gz 4 334.nii.gz 1 12.nii.gz 4 134.nii.gz If entry in 1st column of 1st text file matches the 1st column of 2nd text file, then copy the file (name of which is the second column) associated with... (4 Replies)
Discussion started by: vd24
4 Replies

6. Shell Programming and Scripting

Extracting anchor text and its URL from HTML files in BASH

Hi All, I have some HTML files and my requirement is to extract all the anchor text words from the HTML files along with their URLs and store the result in a separate text file separated by space. For example, <a href="/kid/stay_healthy/">Staying Healthy</a> which has /kid/stay_healthy/ as... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

7. UNIX for Advanced & Expert Users

Extracting the required text from log files

It would be highly appreciable if any one helps me in this. I am trying to get it done through Java but I love unix and believe it can be done within minutes with couple of lines. The input log file is a text file contains multiple entries seperated by a blank line. Each seperated entry... (7 Replies)
Discussion started by: hareeshram
7 Replies

8. Shell Programming and Scripting

processing file names using text files

Hi, I have to perform an iterative function on a set of 10 files. After the first round the output files are named differently than the input files. examples input file name = xxxx1.yyy output file name = xxxx1_0001.yyy I need to rename all of the output files to the original input... (5 Replies)
Discussion started by: ligander
5 Replies

9. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies

10. UNIX for Dummies Questions & Answers

Extracting information from text fields.

Dear friends, I'm a novice Unix user and I'm trying to learn the ropes. I have a big task I have to accomplish and I'm convinced Unix can get the job done, I just haven't figured out how. I recently posted on the topic of cutting text between unique text patterns and somebody helped me a great... (24 Replies)
Discussion started by: spindoctor
24 Replies
Login or Register to Ask a Question