Sed command help (appending)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed command help (appending)
# 8  
Old 08-12-2012
Quote:
Originally Posted by slufoot80
I tried it but it didn't work
If you want help, you need to provide useful feedback. At the very least this should include a transcript of the commands you ran, how you ran them, and the output they generated (especially error messages). All of that information should be included within code tags, to preserve formatting.

Regards,
Alister
# 9  
Old 08-13-2012
Quote:
Originally Posted by Don Cragun
... editing /etc/group, you want ed or ex; not sed:
Please enlighten me: why not sed?
# 10  
Old 08-13-2012
Quote:
Originally Posted by RudiC
Please enlighten me: why not sed?
If /etc/group occupies more than one disk block, it would be safer to use:
Code:
sed "/^$Grp:/s/$/,$Usr/" /etc/group > tmpfile
mv tmpfile /etc/group

to avoid the possibility of /etc/group being corrupt if someone tries to authenticate during the time it takes the "w" command to complete in the ed script I gave, but slufoot80 explicity said:
Quote:
I need to append a user to the end of a group in the /etc/group file
not that he wanted to a add user to the end of a group in a copy of the /etc/group file.

Using the code above, you also have to be sure that tmpfile isn't an existing file and can't be created (hi-jacked) by a hacker between the sed and the mv. Note that you to run this in /etc; if tmpfile is on a different file system than /etc, the mv won't be an atomic operation any more than the w command in ed would have been.
# 11  
Old 08-13-2012
@Don Cragun: Thanks for drawing the attention to these aspects. As I said, tinkering with system files is always a thing to consider twice.
Btw, you should add a s/:,/:/ to your sed cmd in case $Usr would be the first in $Grp.
This User Gave Thanks to RudiC For This Post:
# 12  
Old 08-13-2012
Quote:
Originally Posted by RudiC
@Don Cragun: Thanks for drawing the attention to these aspects. As I said, tinkering with system files is always a thing to consider twice.
Btw, you should add a s/:,/:/ to your sed cmd in case $Usr would be the first in $Grp.
Good catch. And doing that in the ed script would be something like:
Code:
ed -s /etc/group <<-EOF
	/^$Grp:/s/$/,$Usr/p
	.g/:,$Usr$/s//:$Usr/p
	w
	q
EOF

with the .g needed in front of the second s command to preserve the 0 exit status if $Grp was found and $Usr wasn't the first user in the group.

One should also make sure that the script isn't adding a user to the "nobody" group nor the "nogroup" group (or other groups depending on the OS on the machine), but I didn't bother because the idea of editing /etc/passwd, /etc/group, or any corresponding shadow files because usermod won't work (without explaining why usermod won't work) still just seems wrong. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed appending problem

i have a number of java files containing eg: --------------myfile.java-------------- package zip.fun.myfiles; import java.* import something..; import sdfdfdsa; ... ... -------------------------------------------- Now I need to append / insert a line as follows: ... (10 Replies)
Discussion started by: linuxadmin
10 Replies

2. UNIX for Dummies Questions & Answers

sed - appending text

Hey all I am trying to append a file called datebook.txt. I want to append the end of each line containing the name Fred with three ***. I believe I need to make the * loose its meta character meaning in addition to using the append command. I have tried several variations of this command and I... (3 Replies)
Discussion started by: citizencro
3 Replies

3. Shell Programming and Scripting

Awk/Sed - appending within file

hello all, First time post here. I have searched a bit but could not find an exact answer. I have about a week's experience of Sed and Awk, and am having fun, but am a little stuck. I am reformatting an xml file into json format. I have got this far: {"clients": ...and so on. What I want... (22 Replies)
Discussion started by: singerfc
22 Replies

4. Shell Programming and Scripting

sed appending needed only after first instance

Hi, Here is my piece of code used with sed in shell script: sed -i '/<falsemodule-option>/ a\<LdapLogin>' myxmlfile The problem that i am facing with the above is that in 'myxml' file i have mulitple instances of <falsemodule-option> so when i execute the above sed command, it is appending... (10 Replies)
Discussion started by: sunrexstar
10 Replies

5. UNIX for Dummies Questions & Answers

Appending sed output to variable

I want to append matched output and cat the results into an variable. but I've been running into problems. sed is printing result on to screen instead of appending the output to $CAPTURE. I'm stumped...how should i fix this? contents of $TEST 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 expected... (5 Replies)
Discussion started by: jazzaddict
5 Replies

6. UNIX for Dummies Questions & Answers

Sed $ appending to front, not to the end

I keep trying to append some astrix to the end of a line, but it keeps overwriting at the front of the line. These are the originals Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900 Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900 ... (5 Replies)
Discussion started by: DrSammyD
5 Replies

7. Shell Programming and Scripting

appending a file using sed in ksh

i am trying to append a 5 line SGML file(file1) with a 500,000 line SGML file (file2). file1 is a template, so i wish to preserve. i only want to add lines 5 to the end of file2. i have: cp file1 temp1 sed -n '5,$p' file2 >> temp1 when i check the tail of temp1, i consistantly find the... (3 Replies)
Discussion started by: smac
3 Replies

8. Shell Programming and Scripting

appending and sed

Hello, I want to add string #REAL at the end of all lines that contain real numbers. How to do this using sed ? (1 Reply)
Discussion started by: scotty_123
1 Replies

9. Shell Programming and Scripting

Sed appending string using for loop?

Hi All, I have been trying to format a file using sed. I can't seem to get the syntax right. I want to append the string from file1.txt to file1.csv with the final output file being file2.csv, but before the string is appended to the end of each line, first insert a comma. Here is the sed... (2 Replies)
Discussion started by: cstovall
2 Replies

10. UNIX for Dummies Questions & Answers

SED Question -- on appending to a file

:confused: I have a script that Cats a flat database file which contains 12 columns into sed. I want to add a 13th column which includes " ,2005-08-29 " * The date needs to be the current date. This 13th column would be appended to the end of each line. Does anyone have a clue... (5 Replies)
Discussion started by: Redg
5 Replies
Login or Register to Ask a Question