Reputation: 12498
I want to insert a big chunk of code using a bash script.
This is how I got it to work using sed(1);
$ sed "/);/i\
\ \ \ \ 'doctrine' => array(\n\
'driver' => array(\n\
__NAMESPACE__ . '_driver' => array(\n\
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',\n\
'cache' => 'array',\n\
'paths' => array(__DIR__ . '\../src/' . __NAMESPACE__ . '/Entity')\n\
),\n\
'orm_default' => array(\n\
'drivers' => array(\n\
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'\n\
)\n\
)\n\
)\n\
),\n
" config/autoload/local.php;
Note: I had to add \n and \ at the end of each line in order to make it work.
Is there a way to "not" have to do that? To insert plain text? Perhaps a different command?
Edit: my question is how to insert the text without having to add all the backslashes and newlines.
This is the text I need to insert:
'doctrine' => array( 'driver' => array( __NAMESPACE__ . '_driver' => array( 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', 'cache' => 'array', 'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity') ), 'orm_default' => array( 'drivers' => array( __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver' ) ) ) )
Upvotes: 1
Views: 116
Reputation: 58578
This might work for you (GNU sed):
cat <<\! >insert_file
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
)
)
)
)
!
sed '/);/e cat insert_file' file.php
Upvotes: 0
Reputation: 8408
If you insist on using sed
, one way (without using backslash at the end of each line) is to save it to a temporary file, e.g.
cat >tempfile <<'EOF'
code here...
EOF
then use the r
(read) command in sed
sed '/);/ r tempfile' config/autoload/local.php
Upvotes: 0
Reputation: 54592
Here's one way:
sed '/);/i\
'doctrine' => array(\
'driver' => array(\
__NAMESPACE__ . '_driver' => array(\
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',\
'cache' => 'array',\
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')\
),\
'orm_default' => array(\
'drivers' => array(\
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'\
)\
)\
)\
)' file.php
If you're trying to insert a file, try:
sed '/);/ {
h
r replacement.txt
g
N
}' file.php
Upvotes: 2
Reputation: 26329
cat << 'EOF' >> config/autoload/local.php
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
)
)
)
)
EOF
To use the lines with sed, you could print into a variable with read
:
read -d '' multiLineString <<"EOF"
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
)
)
)
)
EOF
Then use $multiLineString
with sed
.
Upvotes: 0