-i
can only be used with sed if you're passing a file, it means "inline replace". Without this, the output of sed would be written to stdout
(usually the console output). With -i
, it does an inline replacement, that is, doing replacements in the file itself.
The next code reads the contents of jasperreports.properties
into the variable $input
(line 1) and finds the string to be replaced (line 2).
On the third line, it outputs the input string and pipes it through sed
for replacement. sed
outputs the string to stdout
which will be caught by $(
and )
, and therefore be stored in $input
.
read input < jasperreports.properties
find=$(grep "$jasper" jasperreports.properties | awk -F"reports/" '{print $2}')
input=$(echo "$input" | sed "s/$find/charts/")
If you want to apply the changes immediately to the file:
find=$(grep "$jasper" jasperreports.properties | awk -F"reports/" '{print $2}')
sed "s/$find/charts/" -i jasperreports.properties
From man sed
:
s/regexp/replacement/
Attempt to match regexp against the pattern space. If
successful, replace that portion matched with replacement. The
replacement may contain the special character & to refer to that
portion of the pattern space which matched, and the special
escapes \1 through \9 to refer to the corresponding matching
sub-expressions in the regexp.
<br>
for a line break, indent a string with four spaces and a newline before it to show it as code.grep | awk
is pointless). It can probably be done with a single awk or sed. If you show some sample data and wanted output we might help you with that.