Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert lines in file - update thread822-1056947 2

Status
Not open for further replies.

AnotherAlan

Technical User
Feb 10, 2006
362
GB
Morning all, I have looked through the threads already pertaining to my question, but I still can't get the syntax correct.
I would like to use a script to add a newly created netgroup to my passwd files. However, as I already have netgroups defined I have a bin/false entry at the tail of each passwd file.
I am attempting to add a new line above this bin/false entry and include the new netgroup.
My problem is that I am useless at expressions and due to the nature of a netgroup entry i.e. +@XXXX it is far too many metacharacters for me to work out the sed or nawk statement. I hope this makes sense.
All help very much appreciated, or else I've got 200 servers to change manually.


 
Hi

I have no idea what is netgroup and how your file looks or should look. Please post samples.

Generally some ways to insert before a line :
Code:
[blue]master #[/blue] seq 3
1
2
3

[blue]master #[/blue] seq 3 | sed '/2/ibefore two'
1
before two
2
3

[blue]master #[/blue] seq 3 | awk '/2/{print "before two"}1'
1
before two
2
3

[blue]master #[/blue] seq 3 | perl -pe 'print "before two\n" if /2/'
1
before two
2
3

[blue]master #[/blue] seq 3 | while read str; do [[ $str == 2 ]] && echo "before two"; echo "$str"; done
1
before two
2
3

Feherke.
 
Thanks Feherke, I'll give those a try.
A netgroup entry is a reference to a pre-defined list of users. The syntax for a netgroup entry starts +@, therefore a list of users in the group opps would have an entry such as +@opps:x::::: listed in the password file.
Netgroups are used with NIS to simplify access.

Thanks for the reply, much appreciated.
Alan
 
Hi,

Here is a small sample;

+@ukadm:x:::::
+:x:::::/bin/false

This is the entry for the passwd file. I would like to add an entry "+@ukops" in the last but one line. i.e It has to go before "+:x:::::/bin/false".

So expected / required output would be;

+@ukadm:x:::::
+@ukops:x:::::
+:x:::::/bin/false

Thanks very much for your help Feherke. I have tried your previous examples and the only one that works for me is the while read statement. I haven't tried to use this with the "+@" characters yet though.
 
Hi

There are small chances to use the [tt]sed[/tt] code. IT works with GNU [tt]sed[/tt], but less likely to work with Unix [tt]sed[/tt]s. The [tt]awk[/tt] code works with [tt]gawk[/tt] and [tt]mawk[/tt]. The [tt]perl[/tt] should work anyway because that is universal. The shell code was tested with [tt]bash[/tt], but should work with [tt]ksh[/tt] too.

In the [tt]sed[/tt], [tt]awk[/tt] and [tt]perl[/tt] codes are used regular expressions, so any line containing the given substring will match. The shell code checks for equality, because I forgot the asterisks ( * ), to use wildcard :
Code:
[blue]master #[/blue] seq 3 | while read str; do [[ $str == [red]*[/red]2[red]*[/red] ]] && echo "before two"; echo "$str"; done
1
before two
2
3
Regarding the "+@", note that the plus sign ( + ) is quantifier in regular expression, so you have to escape it by putting a backslash ( \ ) in front of it.

Feherke.
 
Feherke, Thank you very much. It works with the escaped +.
You have saved me a lot of time.
Please have a star.

Alan
 
Or how about:

[tt]sed -i '$i\
+@opps:x:::::' /etc/passwd[/tt]

Annihilannic.
 
Hi Annihilannic,

Thanks for the post. I'm running Sol 9/10 and don't seem to have the -i switch for sed.
 
In that case leave out the -i and redirect the output to another file (as you would have had to with the other solutions anyway I think?), e.g.

[tt]cp -p /etc/passwd /etc/passwd.`date +%Y%m%d`
sed -i '$i\
+@opps:x:::::' /etc/passwd.`date +%Y%m%d` > /etc/passwd[/tt]

Annihilannic.
 
Thanks again Annihilannic,

I may be being hugely dumb here (as a blonde, this comes as standard), but what does the $i represent? Is this just each pass through the loop? And is this just appending to the end of the file or the last line but one?
Sorry to be a pain.
I did admit to being useless with expressions earlier though.
 
Some sed commands take the syntax:

[tt]<address><command>[\
<parameters>][/tt]

In this case the address "$" refers to the last line of the file. "i" is the command to insert text.

By default sed prints out every line of the file, so what happens in this case is it prints out each line until it gets to the last line, where it inserts the text you want, and then prints out the last line.

Annihilannic.
 
Annihilannic,

Thanks for the explanation and for your time. I understand now. Just been through the man pages and can see what we should be achieving. I am still having difficulty putting this whole expression together with sed though.
I am guessing that it's to do with the '+', as it was previously with Feherke.
I'm getting sed: command garbled errors when trying;
sed '$i\+@ukops'
sed '$i\\+@ukops'

Anyway, thanks again, please don't feel like you need to spend any more of your time with this.
I'll do a little more reading, but your efforts are appreciated.
Alan
 
Got it. Excellent, thanks guys. I should pay closer attention to the formats given.

Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top