ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 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.
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:
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.