I have a file whose contents look like:


I want to replace the name of only ear1.ear with ear2.ear and I am using the below sed command, but with no luck:

sed -i.bak s/*.ear/ear2.ear/g earlist.mps

where earlist.mps is the file name

I have tried:

sed -i.bak s/.*.ear/ear2.ear/g earlist.mps

but this replaces the whole file with ear2.ear.

  • sed -i.bak '\|ear1|s|1\.|2.|' earlist.mps – Costas Sep 25 '15 at 8:33
  • @Costas actually i dont know the name of ear1.ear ...ie old ear – amol singh Sep 25 '15 at 8:37
  • In any way you should exactly indicate what do you want to change – Costas Sep 25 '15 at 9:06

With sed:

sed 's|/[^/]\+\.ear$|/ear2.ear|' file
  • It seaches for line ending with .ear ($ matches the end of the line). The filename can be everything except a /: [^/]\+. The + indicates that a character (which is not a /) can be repreated 1 or more times.
  • Then is replaces that match with /ear2.ear.
| improve this answer | |
  • i guess it wont work if i dont know the name of ear1.ear .. means the old ear – amol singh Sep 25 '15 at 8:35
  • @amolsingh Try it now, i edited the answer – chaos Sep 25 '15 at 8:38
  • its working man ..sorry for late reply ... – amol singh Sep 25 '15 at 9:16
  • but for some reason i am unable to use variable in this eg ... sed -i.bak 's|/[^/]\+\.ear$|/${e}|' earlist.mps – amol singh Sep 25 '15 at 9:22
  • The reason is the single quote, use double quotes instead; sed -i.bak "s|/[^/]\+\.ear$|/${e}|" ... – chaos Sep 25 '15 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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