0

I am using chef recipes for setting java_home. For that i am using sed command for finding JAVA_HOME string in bash_profile and then going to replace that line with

export JAVA_HOME="a/b/java"

so how to replace that line with sed command.

sethvargo
  • 26,739
  • 10
  • 86
  • 156
sandip divekar
  • 1,628
  • 5
  • 22
  • 40

2 Answers2

3

This should work whether or not JAVA_HOME is found

sed -i '/JAVA_HOME/d;$aexport JAVA_HOME="a/b/java"' .bash_profile
Zombo
  • 1
  • 62
  • 391
  • 407
  • 2
    +1; just to make it explicit: this will always place the line at the _end_ of the file; OSX (FreeBSD) version: `sed -i '' -e '/JAVA_HOME/d;$a\'$'\n''export JAVA_HOME="a/b/java"' .bash_profile` – mklement0 May 05 '14 at 15:09
  • Hi steven, I am having another query. If i want to use command at replacement variable. e.g sed -i '/namenode/cnamenode=for a in `cat value.txt | grep namenode | cut -d "=" -f2`; do echo $a; done' output.txt I am runing this command but it is replacing the line with "namenode=for a in `cat value.txt | grep namenode | cut -d "=" -f2`; do echo $a; done" but actually i want "namenode=8080" – sandip divekar May 06 '14 at 09:51
2

Another sed,

sed -i.bak '/JAVA_HOME/s@.*@export JAVA_HOME="a/b/java"@' .bashrc
sat
  • 14,589
  • 7
  • 46
  • 65