Reputation: 11
This is how I am adding text before first pattern reveal:
FILE_NAME="folder/myfile.c++"
STR_TO_ADD="string that i want to add"
PATTERN="banana"
ed $FILE_NAME 2>NULL 1>NULL<<EOF
/^$PATTERN
-1
a
$STR_TO_ADD
.
wq
EOF
but if the line is started with some characters before the "banana" so it is not working. mostly white-speces but i whould like to hear answer for all characters. thanks!
Upvotes: 0
Views: 43
Reputation:
This seems to be doing what (I surmise) you want:
#!/bin/bash
FILE_NAME="folder/myfile.c++"
STR_TO_ADD="string that i want to add"
PATTERN="banana"
ed $FILE_NAME <<EOF
/^$PATTERN
-1
a
$STR_TO_ADD
.
wq
EOF
Upvotes: 0