Reputation: 2921
I'm trying to prepend require_once 'bootstrap.php';
string to each schema.yml
file in specific dirrectory. I'm linux newbie. Could someone show me the one-line-magic-command
?
Any help is very appreciated!
EDIT I need to search for schema.yml recursively.
Upvotes: 2
Views: 1750
Reputation: 1244
Create a quick python script:
prepend_my_text.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
from os.path import join
TEXT = """require_once 'bootstrap.php';\n"""
def main():
for dirpath, _, files in os.walk("."):
for f in files:
if f == "schema.yml":
with file(join(dirpath, f), 'r') as original: data = original.read()
with file(join(dirpath, f), 'w') as modified: modified.write(TEXT + data)
if __name__ == '__main__':
main()
and run it in the directory you are searching:
$ chmod +x prepend.py
$ ./prepend.py
The nice thing is that you can use string literals (triple quotes in python) to avoid worrying about escaping characters
Upvotes: 0
Reputation: 2276
This is a followup to @dogbane 's answer, which I liked, but needed some tweaking to work properly on my Mac. (I tried to post this as a comment on his answer, but I don't think you can put all this formatting in a comment.)
I ended up with:
find somedir -name schema.yml | xargs sed -i '' "1i\\
require_once 'bootstrap.php';
"
Specifically, the changes were:
-i
option to sed
(the extension to use for the backup file for in-place editing) requires an argument, but empty quotes are sufficient (which tells it not to create a backup file).sed
command needed to have a \
after the address (but escaped, because it's in double-quotes, so it ends up as a \\
), then a newline, then the text to add, then another newline, then the close quote.Upvotes: 1
Reputation: 274758
Using sed:
find somedir -name schema.yml | xargs sed -i "1i require_once 'bootstrap.php';"
Upvotes: 6
Reputation: 80433
find somedir -name schema.yml | \
xargs perl -i.orig -pe 'print "require_once \x27bootstrap.yml\x27\n" if $. == 1; close ARGV if eof'
Upvotes: 4
Reputation: 724
You may want to review your spelling of "bootrstap.yml"...
#!/bin/bash
TMPFILE=/tmp/reallyuniquetempnamewhateveryouchoose
for f in `find . -name schema.yml`
do
echo "require_once 'bootrstap.yml';" > $TMPFILE
cat $f >> $TMPFILE
mv -v $TMPFILE $f
done
Edit: well the one-liner above is better, even if it's a bit harder to understand at first :)
Upvotes: 2