Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Using sed to append a file?
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!


Report this thread to moderator Post Follow-up to this message
Old Post
newworldman@email.com
04-15-05 08:59 PM


Re: Using sed to append a file?
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 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Pascal Bourguignon
04-15-05 08:59 PM


Re: Using sed to append a file?
Thanks,

I ended up using ed.

maybe some day this will all make sense...


Report this thread to moderator Post Follow-up to this message
Old Post
newworldman@email.com
04-16-05 01:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:16 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.