-1

I am trying replace a line with another line and add new line below that using a shell script.

I want to replace:

export PATH

with:

JAVA_HOME=/usr/lib/jvm/java-1.8.0-ibm-1.8.0.2.10-1jpp.1.el7.x86_64
export PATH JAVA_HOME

That means, it has to replace replace export PATH with these two lines.

Please help me how can I do with sed or shell script

closed as off-topic by Jeff Schaller, Archemar, Jakuje, Anthon, MelBurslan Apr 21 '16 at 18:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – Jeff Schaller, Archemar, Jakuje, Anthon, MelBurslan

1
sed 's|^export PATH$|JAVA_HOME=/usr/lib/jvm/java-1.8.0-ibm-1.8.0.2.10-1jpp.1.el7.x86_64\
& JAVA_HOME|' < "$yourfile"
0

Use sed command :

sed -i 's/<original text>/<new-text>/g' file.txt


sed -i 's/original/new/g' file.txt

Explanation:

  • sed = Stream EDitor
  • -i = in-place (i.e. save back to the original file) The command string:
  • s = the substitute command
  • original text = a regular expression describing the word to replace (or just the word itself)
  • new text = the text to replace it with
  • g = global (i.e. replace all and not just the first occurrence)
  • file.txt = the file name

Alternative :

awk '{gsub(/'pattern'/,"replacement")}' file

Not the answer you're looking for? Browse other questions tagged or ask your own question.