Home > Uncategorized > copy a file while modifying a value inside it

copy a file while modifying a value inside it

January 14th, 2010 Leave a comment Go to comments

The sed does the filtering, you should be able to see how to adapt this to your own requirements

So I want to duplicate init.php while changing the value of a flag inside it for each copy

cat init.php | sed s/flag=2/flag=3/ > dir3/init.php
cat init.php | sed s/flag=2/flag=4/ > dir4/init.php

If you see the first comment Bawdo suggests rightly that this is a UUOC

sed “s/flag=2/flag=4/”  init.php > dir4/init.php

I’ve always underused sed unfortunately

which then can written (zsh)

for i in {3,4}; sed s/flag=2/flag=$i/ init.php > dir$i/init.php

sed stands for stream editor and therefore cannot do an in place edit this is where perl thrives

perl -p -i -e ‘s/\surname\b/form_surname/g’    *.php

or safer

perl -p -i.bak -e ‘s/\b23\b/34/g’  fred.php  (creates a backup of each file eg fred.bak)

Categories: Uncategorized Tags:
  1. January 14th, 2010 at 23:33 | #1

    You have a UUOC[1] there.

    sed will accept a file as an argument. From the man page:

    sed [OPTION]… {script-only-if-no-other-script} [input-file]…

    [1] http://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat

  1. No trackbacks yet.