shoma_pfc
shoma_pfc

Reputation: 21

Modify the beginning and the end of line if match

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

Answers (3)

Corentin Limier
Corentin Limier

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

William Pursell
William Pursell

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

Matias Barrios
Matias Barrios

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

Related Questions