Reputation: 67
In freebsd 10.2
I have a text file ie. test.txt The file contains The follow lines
Line1
Line2
Line3
option
#i want to add here Mynewline text
Line4
Line5
Line6
Line7
i try sed -i '' 's/option/option\Mynewline/g' test.txt
Line1
Line2
Line3
optionMynewline
Line4
Line5
Line6
Line7
Upvotes: 2
Views: 648
Reputation: 26895
Give a try to:
perl -pi -e 's/option/option\n/' file
or
perl -pi -e 's/option/option\n# my new line/' file
The file will be now something like:
Line1
Line2
Line3
option
# my new line
Line4
Line5
Line6
Line7
By using "perl pie" besides adding only the new line it won't modify the file like with sed
that adds a new \n
if file doesn't end with a \n
(Replace all occurrences without changing file endings)
Upvotes: 0
Reputation: 2601
BSD sed does not interpret '\n' as new line, so you can use an actual newline, escaped by a backslash.
cat f.txt
Line1
Line2
Line3
option
Line4
Line5
Line6
Line7
$ sed -i '' 's/option/option\
Mynewline/g' f.txt
$ cat f.txt
Line1
Line2
Line3
option
Mynewline
Line4
Line5
Line6
Line7
Another option is to install and use gsed.
Upvotes: 0
Reputation: 58351
This might work for you:
sed -i '.bak' -e '/^option/p' -e '//s/.*/myNewLine/' file
Or:
sed -i '' '/^option/{p;s/.*/newline/;}' file
Or:
cat <<\! | sed -i '.bak' -f /dev/stdin file
/option/a\
newline
!
Or:
sed -i '' '/^option/{G;s/$/myNewLine/;}' file
Or:
cat <<\! | sed -i '' -f /dev/stdin file
/option/p
//c\
newline
!
Upvotes: 0
Reputation: 41446
If you can use awk
, this should do:
awk '/option/ {$0=$0"\nMy new line"} 1' file
If option
is found, add new line and text to the line, and then print all
Upvotes: 3