Re: [Hampshire] Quick question

Top Page

Reply to this message
Author: Paul Tansom
Date:  
To: hampshire
Subject: Re: [Hampshire] Quick question
** Keir Whitlock <keir.whitlock@???> [2007-12-20 09:01]:
> Hi,  
>    A quick one for you,  echo "hi" >> /tmp/file will append hi to the 
> end of file, but how do I prepend it to the top of the file?

** end quote [Keir Whitlock]

How's about:

sed -i.bak '1 s/^/Hi\n/' file.txt

- the -i.bak works on the existing file and creates a backup with .bak
added to the file name
- the bit between the ' characters does the actual work
o the 1 at the beginning limits the action to the first line
o then there is the standard style substitution replacing the beginning
(or rather inserting, since you can't exactly replace the beginning -
it still has to start somewhere!) of the line (indicated by the ^)
with Hi and a new line (the \n)
- lastly the file.txt is the file name to work on

I'm far from being an expert, but sed and awk are mighty powerful tools
if you can get to grips with them, this is just scratching the surface
of one of them!

--
Paul Tansom | Aptanet Ltd. | http://www.aptanet.com/ | 023 9238 0001
======================================================================
Registered in England | Company No: 4905028 | Registered Office:
Crawford House, Hambledon Road, Denmead, Waterlooville, Hants, PO7 6NU