insert new line in files

William Gordon Rutherdale will.rutherdale at utoronto.ca
Sat Feb 7 04:39:14 PST 2009


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



More information about the freebsd-questions mailing list