Hi guys,
I am fairly comfortable with using the sed
command if the string to be replaced is all on a single line.
I was wondering is it possible to use sed
command in a multiline way ?
Say for example I have the below string on 2 different lines:
{
"key": "brandNameA",
"value": "aMobile"
}
And I want it to be replaced with:
{
"key": "brandNameB",
"value": "bMobile"
}
Is it possible using a single instance of sed
?
RudiC
2
Try
sed '/brandNameA/ {N; s//brandNameB/; s/aMobile/bMobile/}' file
Unix sed wants a ;
or a newline (or another -e ' '
) before a }
sed '/brandNameA/ {N; s//brandNameB/; s/aMobile/bMobile/;}' file
Further some Unix sed have a bug: a command that follows a }
must be separated by a newline (or another -e ' '
).
Aia
4
Perhaps a few other alternatives:
sed '/brandNameA/ {N; s/brandNameA\(.*\n.*\)aMobile/brandNameB\1bMobile/;}' junaid_subhani.example
sed '/brandNameA/ {N; s/\(brandName\)A\(.*\n.*\)a\(Mobile\)/\1B\2b\3/;}' junaid_subhani.example
sed '/brandNameA/ {N;s/A\(.*\n.*\)a/B\1b/;}' junaid_subhani.example