LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2015, 12:12 PM   #1
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Rep: Reputation: 15
Script to generate 200 files


I have a configuration file for Nagios that I am trying to create contact information for each of the Offices I support, 200:
define contact {
contact_name Office1
alias Office1
host_notification_period xi_timeperiod_24x7
service_notification_period xi_timeperiod_24x7
host_notification_options d,r,
service_notification_options c,r,
host_notification_commands xi_host_notification_handler
service_notification_commands xi_service_notification_handler
email office1@domain.com
}

I have a two column list that has the Office name which I want to replace Office1 with and the Office email address that I want to replace Office1@domain.com with. I would like to have each Office as a separate file.

I tried to use something like this:
for f in /path/to/Office information
do
sed -e s/Office1/MyRealOfficeName/g < "$f" > "MyRealOfficeName.cfg"
done

As you can tell it is a hot mess as I am trying to read the "template" and then replace it with the two column list file I have to replace the fields. It sounds so simple but I am in need of some serious help.

Thanks!
 
Old 07-21-2015, 12:30 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
No real expert here, but won't it work properly if you have the sed line as something like:
Code:
sed -e s/Office1/MyRealOfficeName/g $f > "MyRealOfficeName.cfg"
I don't think you need the "<" nor quotes around the $f variable.

And then is MyRealOfficeName to be different per office? You'll need to traverse a list or array of office names or for each entry set up a var with the proper string, right?
 
Old 07-21-2015, 12:32 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Rather than doing sed just put what you want into the output of the script:

Code:
cat /path/to/Office information |while read offc mail
do echo "define contact {"
   echo "contact_name $offc"
   echo "alias $offc"
   echo "host_notification_period xi_timeperiod_24x7"
   echo "service_notification_period xi_timeperiod_24x7"
   echo "host_notification_options d,r,"
   echo "service_notification_options c,r,"
   echo "host_notification_commands xi_host_notification_handler"
   echo "service_notification_commands xi_service_notification_handler"
   echo "email $mail"
   echo "}"
   echo ""
done >>MyRealOfficeName.cfg

Last edited by MensaWater; 07-21-2015 at 12:33 PM.
 
Old 07-21-2015, 12:37 PM   #4
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Original Poster
Rep: Reputation: 15
MensaWater I never thought about doing that. In my Office information file I have two columns.
Office Name and Office Email
Office1 office1@domain.com
Office2 office2@domain.com

Do I need to mark the columns offc and mail? Not following how each Office will get a cfg file created for their specific Office. So in the end I would have an office1.cfg, office2.cfg, etc.
 
Old 07-21-2015, 12:56 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
without seeing a sample of your input file this is a stab in the dark:
Code:
cat "/path/to/Office information" | while read offc mail
do
   echo -e "define contact {\n \
   contact_name $offc\n \
   alias $offc\n \
   host_notification_period xi_timeperiod_24x7\n \
   service_notification_period xi_timeperiod_24x7\n \
   host_notification_options d,r,\n \
   service_notification_options c,r,\n \
   host_notification_commands xi_host_notification_handler\n \
   service_notification_commands xi_service_notification_handler\n \
   email $mail\n \
  }\n" > $offc.cfg
done

Last edited by schneidz; 07-21-2015 at 12:57 PM.
 
Old 07-21-2015, 01:16 PM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by bkone View Post
MensaWater I never thought about doing that. In my Office information file I have two columns.
Office Name and Office Email
Office1 office1@domain.com
Office2 office2@domain.com

Do I need to mark the columns offc and mail? Not following how each Office will get a cfg file created for their specific Office. So in the end I would have an office1.cfg, office2.cfg, etc.
No the "while" loop starting with the "while" and ending with the "done" is assigning two variables for each line. Office1 = $offc and office1@domain.com = $mail on first pass because that is the variable the while assigns to each column. On next pass Office2 = $offc and office2@domain.com = $mail. On third pass the two columns on 3rd line will become $offc and $mail etc...

However if your actual mail address columns literally contains "[email]" and "[/email}" in the second column you'd have to modify the script to strip that out:

Code:
cat test.txt |while read offc mail
do stripmail=$(echo $mail |awk -F] '{print $2}' |awk -F[ '{print $1}')
   echo "define contact {"
   echo "contact_name $offc"
   echo "alias $offc"
   echo "host_notification_period xi_timeperiod_24x7"
   echo "service_notification_period xi_timeperiod_24x7"
   echo "host_notification_options d,r,"
   echo "service_notification_options c,r,"
   echo "host_notification_commands xi_host_notification_handler"
   echo "service_notification_commands xi_service_notification_handler"
   echo "email $stripmail"
   echo "}"
   echo ""
done >>MyRealOfficeName.cfg

Last edited by MensaWater; 07-21-2015 at 01:27 PM.
 
Old 07-21-2015, 07:51 PM   #7
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
a here document way to embed text into a shell script, version
Code:
#!/bin/bash
cat test.txt |while read offc mail
do stripmail=$(echo $mail |awk -F] '{print $2}' |awk -F[ '{print $1}')
cat << END_TEXT
define contact {
contact_name $offc
alias $offc
host_notification_period xi_timeperiod_24x7
service_notification_period xi_timeperiod_24x7
host_notification_options d,r,
service_notification_options c,r,
host_notification_commands xi_host_notification_handler
service_notification_commands xi_service_notification_handler
email $stripmail
}
END_TEXT
done >>MyRealOfficeName.cfg

Last edited by genss; 07-21-2015 at 07:57 PM. Reason: updated to the real thing
 
1 members found this post helpful.
Old 07-22-2015, 07:20 AM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Nice here document usage Genss.

I'd suggest adding a blank before the END TEXT:

Code:
}

END_TEXT
That way there's a single blank line between each of the contacts in the output. It makes the config file easier to review later.

One question though: Would the external "cat" command called in your here document usage be less efficient than the internal bash echo command?

Last edited by MensaWater; 07-22-2015 at 01:20 PM.
 
Old 07-22-2015, 11:24 AM   #9
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Original Poster
Rep: Reputation: 15
Thanks to all! That worked and has saved me countless hours.

Love this Community!
 
Old 07-22-2015, 01:19 PM   #10
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Glad to hear we helped. If you would please go to thread tools and marked this solved. That way others in future that find your post will see it as a resolved issue and may more quickly resolve their own issues using it.
 
Old 07-22-2015, 07:58 PM   #11
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
Quote:
Originally Posted by MensaWater View Post
One question though: Would the external "cat" command called in your here document usage be less efficient than the internal bash echo command?
echo doesn't seem to work with here documents

efficiency, idk
i think the echo way would be faster
that said i was reading awk the other day and it seems to have been made for these kinds of text processing problems

edit:
why not just use echo like
Code:
cat test.txt |while read offc mail
do stripmail=$(echo $mail |awk -F] '{print $2}' |awk -F[ '{print $1}')
echo "define contact {
contact_name $offc
alias $offc
host_notification_period xi_timeperiod_24x7
service_notification_period xi_timeperiod_24x7
host_notification_options d,r,
service_notification_options c,r,
host_notification_commands xi_host_notification_handler
service_notification_commands xi_service_notification_handler
email $stripmail
}

"
done >>MyRealOfficeName.cfg

Last edited by genss; 07-22-2015 at 08:07 PM.
 
Old 07-23-2015, 09:58 AM   #12
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by genss View Post
why not just use echo like
Code:
cat test.txt |while read offc mail
do stripmail=$(echo $mail |awk -F] '{print $2}' |awk -F[ '{print $1}')
echo "define contact {
contact_name $offc
alias $offc
host_notification_period xi_timeperiod_24x7
service_notification_period xi_timeperiod_24x7
host_notification_options d,r,
service_notification_options c,r,
host_notification_commands xi_host_notification_handler
service_notification_commands xi_service_notification_handler
email $stripmail
}

"
done >>MyRealOfficeName.cfg
That looks like it would work. I'm an old UNIX hand so am used to the days echo was only an external command. (Even in Linux to get newline ("\n") one has to use the external echo command with the -e flag.) I'm used to either inserting the "\n" between items or doing echo on each line and had never tried to do the multiple line echo you suggest.

Of course one could use bash's printf instead as well but there are dozens of ways to do everything in Linux.

Last edited by MensaWater; 07-23-2015 at 10:00 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to generate EXCEL sheet by shell script dani1234 Linux - Newbie 15 09-14-2014 07:36 PM
[SOLVED] Need script to generate vouchers suse_nerd Programming 3 06-25-2010 10:13 AM
shell script to auto process ten random files and generate logs novice82 Linux - Newbie 4 10-05-2009 07:08 AM
Add Text to Beginning of 200 Files dougp23 Linux - Newbie 3 10-30-2006 03:01 PM
Best way to back up about 200 GB of files to DVDs? Electrode Linux - Software 9 09-09-2003 04:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration