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! |
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" 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? |
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 |
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. |
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 |
Quote:
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 |
a here document way to embed text into a shell script, version
Code:
#!/bin/bash |
Nice here document usage Genss.
I'd suggest adding a blank before the END TEXT: Code:
} One question though: Would the external "cat" command called in your here document usage be less efficient than the internal bash echo command? |
Thanks to all! That worked and has saved me countless hours.
Love this Community! |
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.
|
Quote:
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 |
Quote:
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. |