Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 07 Feb 2009 07:39:02 -0500
From:      William Gordon Rutherdale <will.rutherdale@utoronto.ca>
To:        freebsd-questions@freebsd.org
Subject:   Re: insert new line in files
Message-ID:  <498D80E6.20702@utoronto.ca>
In-Reply-To: <20090207100621.X49625@wojtek.tensor.gdynia.pl>
References:  <498CBEBE.7080702@gmail.com> <20090207100621.X49625@wojtek.tensor.gdynia.pl>

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.

-Will




Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?498D80E6.20702>