Mailing List Archive
tlug.jp Mailing List tlug archive tlug Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]Re: [tlug] Adding text to the beginning of a file
- Date: Sat, 17 Mar 2007 01:26:47 +0900
- From: "Stephen J. Turnbull" <turnbull@example.com>
- Subject: Re: [tlug] Adding text to the beginning of a file
- References: <45FA6B45.3040500@example.com> <ba75897e0703160413oc5219d7gb2bea3d53cd0afc2@example.com>
Arwyn Hainsworth writes: > On 16/03/07, Dave M G <martin@example.com> wrote: > > Can I wipe out everything up to and including the "--" and then past in > > new text? > Sed is your friend: > > sed '1,/^#--/ d' < file_with_data_to_replace > tmp_file > cat file_with_new_data tmp_file > end_result_file Note that (as in all regexps) whitespace is significant. So the target regexp needs to be at least '^# --'. Note that the classic sig separator uses significant whitespace to help disambiguate: '^-- $'. I would suggest matching that seperator exactly: '^# --$'. This will help to avoid grief someday when (eg) you include a commented-out mail message (such as this one, except I currently have no sig :-), where the commented mail sig separator matches '^# -- $' -- and '^# --' as well. You'd be amazed how often the naked eye misses such details when debugging. Also, something that might be important in writing a "secure" script that doesn't have write permission on any directory in particular, just that file, you can cat stdin explicitly by using '-' as the file name (and if you actually have a file named '-' in the current directory, you must qualify it explicitly: './-'). Thus: sed '1,/^#--/ d' < the_file | cat new_head - > the_file Note that this may not be robust, although it Works For Me (with bash 2.05b.0(1)-release on Mac OS X). Those familiar with stdio redirection will know that '> the_file' truncates the_file before opening it and passing the fd to the process, but apparently sed gets to open the file before the shell truncates it for cat. This may be a race condition that happens to work out, or it may be required by POSIX, any shell-heads out there that know?
- References:
- [tlug] Adding text to the beginning of a file
- From: Dave M G
- Re: [tlug] Adding text to the beginning of a file
- From: Arwyn Hainsworth
Home | Main Index | Thread Index
- Prev by Date: Re: [tlug] Adding text to the beginning of a file
- Next by Date: Re: [tlug] Adding text to the beginning of a file
- Previous by thread: Re: [tlug] Adding text to the beginning of a file
- Next by thread: Re: [tlug] Adding text to the beginning of a file
- Index(es):
Home Page Mailing List Linux and Japan TLUG Members Links