Home > Archive > Unix Programming > April 2005 > Using sed to append a file?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Using sed to append a file?
|
|
| newworldman@email.com 2005-04-15, 3:59 pm |
| After being thoroughly by 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!
| |
| Pascal Bourguignon 2005-04-15, 3:59 pm |
| newworldman@email.com writes:
> After being thoroughly by 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 command:
$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.
| |
| newworldman@email.com 2005-04-15, 8:58 pm |
| Thanks,
I ended up using ed.
maybe some day this will all make sense...
|
|
|
|
|