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



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

sed 's|^export PATH$|JAVA_HOME=/usr/lib/jvm/java-1.8.0-ibm-\
& JAVA_HOME|' < "$yourfile"

Use sed command :

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

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


  • 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.