[SOLVED] Text on a specific line at the end of a line
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'm trying to add text to a file for a specific group of users, I'll need to do examples as I can't think of an easy way of explaining, my file is like this:
Code:
users{
user1
user2
}
group1{
members user2
}
group2{
members user2
}
At the present my code lists all the available groups, how would I add a user to a specified group? (e.g add "members user3") to the end of group 1 for example. So the code ends up like this
Code:
members user2,user3
If I've expained this crap I'm sorry, it's for nagios (if that helps at all).
Last edited by genderbender; 07-26-2010 at 07:37 AM.
Click here to see the post LQ members have rated as the most helpful post in this thread.
sed '/group1/,/}/s/\(members .*\)/\1,user3/' infile
You can tell sed to use a certain range, which can be set as line numbers (3,12) or as a regular expression (/start/,/stop/). The range set in the above sed statement looks for lines between group1 and }. In that range a normal search and replace is done (the s/../../ part).
The green part is somewhat special in this case. I used back-referencing (what is found between \( and \) can be represented as \1 in the replace part).
Example run:
Code:
$ cat infile
users{
user1
user2
}
group1{
members user2
}
group2{
members user2
}
$ sed '/group1/,/}/s/\(members .*\)/\1,user3/' infile
users{
user1
user2
}
group1{
members user2,user3
}
group2{
members user2
}
Hope this helps.
Last edited by druuna; 07-26-2010 at 08:18 AM.
Reason: Simplefied regexp
@grail: the .* part does not work, you do need to use the [a-z0-9 ][[a-z0-9, ]* regexp. Has to do with sed keeping the complete range which is found in the buffer.
So this would work: sed '/group1/,/}/s/members[a-z0-9 ][[a-z0-9, ]*/&,user3/' infile
Ignore. My bad, I used a dos input file........
I'm crap with sed, always need help with it when doing anything remotely challenging. The one liners not working yet but it's probably something stupid I'm doing (the groups and users are variables).
Thanks a lot for the code though guys, as always I really appreciate the effort
I'll keep you posted and let you know if I'm too dumb to fix my own code, haha.
you can use (pass in) variables in the LHS of the sed for sure, and IIRC the RHS too; however, to do it, it simplifies things to put the entire sed code inside "double Quotes" - this saves you needing weird cryptic escapes of everything.
Hmmm, still not getting too far: Output and code below
Code:
Administrators
John
define contact{
contact_name John
user generic-contact
alias John Doe
email john@doe.com
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
register 0
}
define contactgroup{
contactgroup_name Administrators
alias Administrators
members
}
Here's the code which generate the file:
Code:
echo $i
echo $FNAME
sed "/${i}/,/}/s/\(members .*\)/\1,${FNAME}/" $CONTACTCFGFILE
I don't think your initial question and the data in post #10 match.
The first box in post #10, is that the input file (first 2 lines) and the file you want to change?
If so: none of the answers given earlier will work. There is no range Administrators to }. Administrators is not unique and can therefore not be used. define contactgroup{ would be unique, as is define contact{.
Could you elaborate about what it is you want to do. What would the file look like after you have done "your thing" (input file and output file) and how would the script know the content of certain variables? Are these in the input file as well or in the script or given as option to the script?
The first 2 lines are just so you're able to see what the variables currently are - these variables generate the file specified below them based on user input. There can be multiple contactgroups but the contact_group_name is always unique (Administrators was just an example). In order, the following happens (this is psuedo code).
if file exists
ask user their name
if user exists in file exit with an error
if they dont exist add them to the file
check for all the contactgroups in the file
if none exist create a default contactgroup called administrators
display all present contactgroups in the file and allow the user to specify which contact group they want to join
add their name to the end of the members line
if file doesnt exist
do the same as above filling in sections with default text (user input for username and contactgroup still needed)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.