4.18.2012

one-liner 'in-place' file edits

several ways to skin ze cat, and get a tasty snack:

$ cat alchemy
turn lead to gold

python:
$ python -c "import sys,fileinput,re;[sys.stdout.write(re.sub('lead','sardines',line)) for line in fileinput.input('alchemy',inplace=1)]"

$ cat alchemy
turn sardines to gold

perl:
$ perl -p -i -e"s#gold#cracker topping#" alchemy
$ cat alchemy
turn sardines to cracker topping

gnu sed:
$ sed -i -e "s/sardines/cream\ cheese/" alchemy
$ cat alchemy
turn cream cheese to cracker topping

good ol' ed:
$ printf '%s\n' ',s/cracker\ topping/cheesecake/' w q | ed -s alchemy
$ cat alchemy
turn cream cheese to cheesecake

So, the next time you are asked to turn lead to cheesecake, it will be no real problem.




No comments: