bcbishop
bcbishop

Reputation: 2313

sed append text after match

I am writing a shell script to be able to append text after the match is found in a file

for example, in ~/.bash_profile file for the following line

PATH=$PATH:$HOME/bin

we need to append it with :/usr/java/jdk1.6.0_38/bin

so it'll become the following

PATH=$PATH:$HOME/bin:/usr/java/jdk1.6.0_38/bin

how could I do it with sed?

I tried with the following command from inside the console first, but it gave me error complaining 'sed: -e expression #1, char 13: unknown option to `s''

sed '/PATH/s/$/:/usr/java/jdk1.6.0_38/bin' ~/.bash_profile

what's wrong with my command above?

Upvotes: 0

Views: 4033

Answers (2)

potong
potong

Reputation: 58420

This might work for you (GNU sed):

sed 's|PATH=$PATH:$HOME/bin|&:/usr/java/jdk1.6.0_38/bin|' ~/.bash_profile

Upvotes: 3

Thor
Thor

Reputation: 47099

The problem is that you have regex delimiters in the replacement part of the substitute command. Either escape them with \ or use a different delimiter (comma in this case):

sed '/PATH/ s,$,:/usr/java/jdk1.6.0_38/bin,' ~/.bash_profile

Upvotes: 3

Related Questions