Reputation: 21
Is there a way to replace all lines in a file, from
title={WHATEVER_IS_INSIDE}
to
title={{WHATEVER_IS_INSIDE}}
and to preserve whatever is inside unmodified. Note: WHATEVER_IS_INSIDE is always a different strig, so I would need * or so...
Upvotes: 0
Views: 61
Reputation: 5006
Input input.txt
:
title={WHATEVER_IS_INSIDE}
hello
title={IS_INSIDE_WHATEVER}
world
Command :
sed '/title={.*}/s,{,{{,g;/title={.*}/s,},}},' input.txt
Gives you on stdout :
title={{WHATEVER_IS_INSIDE}}
hello
title={{IS_INSIDE_WHATEVER}}
world
Upvotes: 1
Reputation: 212268
You could do:
sed -e '/^title={[^}]*}$/s/{\(.*\)}/{{\1}}/'
or:
sed -E -e '/^title={[^}]*}$/s/{(.*)}/{{\1}}/'
and you'd probably be okay with the simpler:
sed -E -e '/title=/s/{(.*)}/{{\1}}/'
or:
awk '/^title=/{$2=sprintf("{%s}",$2)}1' FS== OFS==
Upvotes: 0
Reputation: 5056
You can do this :
echo "title={WHATEVER_IS_INSIDE}" | sed -E "s/({)([a-zA-Z0-9_]+)(})/\1\1\2\3\3/g"
Output
title={{WHATEVER_IS_INSIDE}}
Upvotes: 0