Is it possible using sed
or awk
or any other shell commands to recognise lines with single character e.g. )
and then replace it with something else?
There could be many lines containing )
but it replaces only lines where there is single character )
.
Example:
exp = ... +
( x
+ 2*(y)
+ z
)
+ ...
What I am trying to get is the following
exp = ... +
( x
+ 2*(y)
+ z
}
+ ...
If I use normal sed command to replace )
it will replace all the matches with )
.
sed -i -e 's/)/\}/g' file
will give
exp = ... +
( x
+ 2*(y}
+ z
}
+ ...
Is there a way out?
EDIT: an example with nested parenthesis where a single line contain inner nested bracket, the solution does not work (for this example):
sed -e 's/(\(.*\))/{\1}/g'
...
+ LNb * (
+ ( - 224/27 + ( - 8/3)*Lqf^2 + (80/9)*Lqf)*CF*NF
+ (1616/27 - 56*[Zeta[3]] + ( - 536/9 + 16*[Zeta[2]])*Lqf + (44/3)*
Lqf^2)*CF*CA
+ ((32 - 128*[Zeta[2]])*Lqf)*CF^2
)
+ ...
which gives
...
+ LNb * (
+ { - 224/27 + ( - 8/3)*Lqf^2 + (80/9)*Lqf}*CF*NF
+ {1616/27 - 56*[Zeta[3]] + ( - 536/9 + 16*[Zeta[2]])*Lqf + (44/3}*
Lqf^2)*CF*CA
+ {(32 - 128*[Zeta[2]])*Lqf}*CF^2
)
+ ...
unlike
...
+ LNb * {
+ ( - 224/27 + ( - 8/3)*Lqf^2 + (80/9)*Lqf)*CF*NF
+ (1616/27 - 56*[Zeta[3]] + ( - 536/9 + 16*[Zeta[2]])*Lqf + (44/3)*
Lqf^2)*CF*CA
+ ((32 - 128*[Zeta[2]])*Lqf)*CF^2
}
+ ...
whereas I know how to avoid it, for example I can merge lines so that it will become a single line and then I can use the command to replace the outermost (...)
to {...}
. However those have some other consequences not particular to this problem.
My main query is not to work it out for these examples but to understand if it is possible to detect and replace lines containing only one character )
in a file and nothing else, where the file may have many other occurrences of )
with some characters which will be intact.