LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to generate 200 files (https://www.linuxquestions.org/questions/programming-9/script-to-generate-200-files-4175548549/)

bkone 07-21-2015 12:12 PM

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!

rtmistler 07-21-2015 12:30 PM

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?

MensaWater 07-21-2015 12:32 PM

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


bkone 07-21-2015 12:37 PM

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.

schneidz 07-21-2015 12:56 PM

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


MensaWater 07-21-2015 01:16 PM

Quote:

Originally Posted by bkone (Post 5394408)
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


genss 07-21-2015 07:51 PM

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


MensaWater 07-22-2015 07:20 AM

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?

bkone 07-22-2015 11:24 AM

Thanks to all! That worked and has saved me countless hours.

Love this Community!

MensaWater 07-22-2015 01:19 PM

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.

genss 07-22-2015 07:58 PM

Quote:

Originally Posted by MensaWater (Post 5394805)
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


MensaWater 07-23-2015 09:58 AM

Quote:

Originally Posted by genss (Post 5395094)
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.


All times are GMT -5. The time now is 08:36 AM.