0

I want to replace the following line:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "patient")

with this line:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "patient", cascade = CascadeType.ALL)

I prefer to use sed but I cannot make it work for me due to these special characters. Any body can help me?

Jotne
  • 40,548
  • 12
  • 51
  • 55
peterboston
  • 877
  • 1
  • 12
  • 24
  • If you add the [code you have tried so far](http://stackoverflow.com/help/mcve) you help people answering your question. – kalyfe May 07 '15 at 18:20
  • See http://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed/29626460#29626460 – Ed Morton May 07 '15 at 19:12

2 Answers2

1

If it's just that one line, you could say

sed 's/)$/, cascade = CascadeType.All)/'

to insert , cascade = CascadeType.All) before the closing regex ($ matches the end of the line here).

I suspect, though, that you have a file in which there are more lines that should remain unchanged, and that you want the substitution to happen only in this line. In this event, you'll need some criterium that uniquely identifies the line -- most commonly a regex. If, for example, this is the only line that begins with @OneToMany(, you could use

sed '/^@OneToMany(/ s/)$/, cascade = CascadeType.All)/'

This will instruct sed to attempt the substitution only if the current line matches the regex ^@OneToMany(. If there are other lines that begin this way and should remain untouched, find a regex that uniquely identifies the line and use it instead.

Wintermute
  • 42,983
  • 5
  • 77
  • 80
0

Here is an awk version:

awk '/@OneToMany/ {sub(/\)/,", cascade = CascadeType.ALL&")}1' file
@OneToMany(fetch = FetchType.LAZY, mappedBy = "patient", cascade = CascadeType.ALL)
Jotne
  • 40,548
  • 12
  • 51
  • 55