Code Comments
Programming Forum and web based access to our favorite programming groups.After being thoroughlyby reading some online sed tutorials, I haved decided post here. I need to understand how to append a file after the first line with new text and append the end of the file with multiple lines of text. This needs to be called from a script where I can set a variable to be used as part of the text. This script is also desired because I need to run it on over 50 systems. Specifically: var=`hostname` I need to append the etc/sudoers file to: append after the first line "User_Alias ALIAS=username, username2 and at the end of the file: "ALIAS=$var=/dir/file /dir/file2 thanks in advance, poor unix admin wannabe!
Post Follow-up to this messagenewworldman@email.com writes: > After being thoroughlyby reading some online sed tutorials, I > haved decided post here. > > I need to understand how to append a file after the first line with new > text > and append the end of the file with multiple lines of text. > > This needs to be called from a script where I can set a variable to be > used as part of the text. > > This script is also desired because I need to run it on over 50 > systems. > > Specifically: > var=`hostname` > I need to append the etc/sudoers file to: > append after the first line "User_Alias ALIAS=username, username2 > > and at the end of the file: > "ALIAS=$var=/dir/file > /dir/file2 > > thanks in advance, > > poor unix admin wannabe! man sed man ed info emacs Well, sed means Stream EDitor, so it's normal modus operandi is to get an input stream, modify the data on the fly and write the result on the output stream. To add a line after the first you'd write the following sed command: 1a\ New line To add new lines after the end of the file, you'd write the following comman d: $a\ newline-1\ newline-2\ ...\ newline-n To do both, you put both commands in a file a.sed: 1a\ New line $a\ newline-1\ newline-2\ ...\ newline-n Then you run: sed -f a.sed < input > output There are some sed variant with an option -i to allow editing one file in place, but not mine, so I can't say how to use it. To edit a file in place, I'd use ed(1), which accepts about the same commands as sed, but doesn't work on streams, but takes the commands from the standard input stream: ed file <<'EOF' 1a New line . $a newline-1 newline-2 ... newline-n . wq EOF Or put the commands in a file a.ed: 1a New line . $a newline-1 newline-2 ... newline-n . wq and use: ed file < a.ed There's no error handling... If you want something more reliable, you should use emacs rather. -- __Pascal Bourguignon__ http://www.informatimago.com/ Grace personified, I leap into the window. I meant to do that.
Post Follow-up to this messageThanks, I ended up using ed. maybe some day this will all make sense...
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.