-1

I need some inputs on how to achieve this:

I need to replace a text in a file, using shell script, the text which i need to replace ends with .ear, for example below:

/home/export/files/list/aa_bb_cc.ear

The shell script should replace the aa_bb_cc.ear with say, replaced.ear.

That is the line after substitution should be:

/home/export/files/list/replaced.ear

I did read online about this, and came to know about sed command. The problem which i have, is that i don't know before hand what the text to be replace would be, that is, i know the text to be replace would be something *.ear (in an attempt to match aa_bb_cc.ear)

Now, how can I do this? I tried to use "*" in sed however it didn't work

CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • 2
    You need to learn about regular expressions. – Alex Hall Mar 23 '16 at 19:34
  • "How do I rename files" is barely a programming question. It's especially not a programming question if you haven't included your attempt to solve the problem. Try to show us your work so far in an [MCVE](http://stackoverflow.com/help/mcve), the result you were expecting and the results you got, and we'll help you figure it out. An answer on StackOverflow needs a question with source code in it. – ghoti Mar 23 '16 at 19:57

2 Answers2

1

With GNU sed:

sed 's|[^/]*\.ear|replaced.ear|' file

or

sed 's|[^/]*\(\.ear\)|replaced\1|' file

If you want to edit your file "in place" use sed's option -i.


See: The Stack Overflow Regular Expressions FAQ

Community
  • 1
  • 1
Cyrus
  • 84,225
  • 14
  • 89
  • 153
1
$ echo /home/export/files/list/aa_bb_cc.ear | sed 's/[^/]*\.ear/xxx.ear/'
/home/export/files/list/xxx.ear

since regex match is greedy, you need to specify to match non-slash char. Dot matches any char, so to specify literal dot you have to escape with back-slash.

karakfa
  • 66,216
  • 7
  • 41
  • 56