Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 07 Feb 2009 07:39:02 -0500
From:      William Gordon Rutherdale <>
Subject:   Re: insert new line in files
Message-ID:  <>
In-Reply-To: <>
References:  <> <>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
Wojciech Puchar wrote:
>> I want to insert a new line of text at a certain position in certain 
>> files recursively under a directory.  More specifically, I want text 
>> like this:
>> include('/usr/home/www/imp-sites/default_inventory.php');
> write a script:
> #!/usr/local/bin/bash
> (a=0
> while [ $a -lt 36 ];do
>  read line
>  echo "$line"
>  a=$[a+1]
> done
> echo "include('/usr/home/www/imp-sites/default_inventory.php');"
> while read line;do
>  echo "$line"
> done) <$1 >/tmp/$$
> mv -f /tmp/$$ $1
> run it over each file

I would avoid this method because it is extremely inefficient, 
especially with large files.
  Shell is very slow for loops like that because it is purely 
interpretive.  If you wrote the same thing in Perl it would run way 
faster just because it does a compile on load.  (Besides, you still have 
to show the 'find' invocation to run it recursively, as requested.)  
Steve's sed solution is probably not bad as well.


Want to link to this message? Use this URL: <>