LinuxQuestions.org
Help answer threads with 0 replies.
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-26-2010, 07:32 AM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
Text on a specific line at the end of a line


Hello!

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.
Old 07-26-2010, 07:54 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

A sed solution:

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
 
2 members found this post helpful.
Old 07-26-2010, 08:07 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
I am with druuna on using sed. My alternative is along the same lines:
Code:
sed '/group1/,/}/s/members.*/&,user3/' infile
Here, as opposed to back referencing I am using another sed nicety where it stores the found item in the variable &
 
2 members found this post helpful.
Old 07-26-2010, 08:14 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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........

Last edited by druuna; 07-26-2010 at 08:16 AM.
 
Old 07-26-2010, 08:15 AM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
I'm with both RE: sed being the tool du jour - and am thanking grail for the reminder about the "&" storing the item.
 
Old 07-26-2010, 08:18 AM   #6
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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.
 
Old 07-26-2010, 08:19 AM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
*

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.
 
1 members found this post helpful.
Old 07-26-2010, 08:21 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Maybe this will help to get you going again:

Code:
#!/bin/bash

AddUser="user5"

ToGroup="group1"

sed "s/${ToGroup}/,/}/s/\(members .*\)/\1,${AddUser}/" infile
Hope this helps.

Last edited by druuna; 07-26-2010 at 10:52 AM. Reason: fixed typo
 
Old 07-26-2010, 08:30 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk -vRS="\n}" '/group1/{ $0=$0",user3" }1' ORS="\n}" file
 
1 members found this post helpful.
Old 07-26-2010, 08:45 AM   #10
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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
 
Old 07-26-2010, 08:54 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

What is the content of both $i and $FNAME? Where are those set? (the i=XYZ and FNAME=ABC parts).

Have a look at post #8
 
Old 07-26-2010, 08:56 AM   #12
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
The code output in the first box shows you, basically $i is Administrators and $FNAME (first name) is a username, in this case John.
 
Old 07-26-2010, 09:11 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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?
 
Old 07-26-2010, 09:22 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Yeah sorry gb, but you have changed the input to which all our little commands were looking for
 
Old 07-26-2010, 09:23 AM   #15
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM

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

All times are GMT -5. The time now is 11:43 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