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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add content to a file 1

Status
Not open for further replies.

olli2003

Technical User
Jan 31, 2003
93
DE
DEAR ALL

You know it's possible to add a content to an old file by using '>>'.
The new text or content will add.
F. EX. you will add at text 'def' to a text 'abc',
the new file will be:

abc
def

My question now, is it possible to add a content in the first row of the old one?

F. EX. if I want to add content 'def', like in this case,
the new file should be:

def
abc

Thanks in advance!

Regards
Oliver
 
Why don't you output your new content to a file, and then append the old content?
 
Hi!

Thanks for all the information,
but what on earth is 'SED'?

Thanks in advance!

Regards
Oliver
 
Sed is a stream editor. It's like a a batch vi.

try:
Code:
man sed
man ed


However, doing it like jprabaker suggested would be much easier.
 
Hello hfaix

Thanks for yor help!

Regards
Oliver
 
Another approach:

Code:
echo "abc" > testfile

export NEWTEXT
NEWTEXT="def"

perl -i.bak -pe 'print "$ENV{NEWTEXT}\n" if $. == 1;' testfile

This performs an "in-place" edit of testfile, automagically handling the copying and renaming for you. After execution, "testfile" will have "def" prepended as the first row, and testfile.bak will have the original content. Use "-i" without the ".bak" if you don't want to preserve the original. The shell variable must be exported for the perl process to inherit it.

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
HI RodKnowlton

Great, that is what I want!

Thanks a lot,


Regards
Oliver
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top