1

I have: A file called 'fooBar'. A variable called 'myVar', which has the string 'the lazy' assigned to it. I want to insert $myVar into the file fooBar before the text '</dog>'

fooBar Before:

The quick brown fox jumps over </dog>

fooBarAfter:

The quick brown fox jumps over the lazy </dog>

What is the best way to achieve this please?

anubhava
  • 664,788
  • 59
  • 469
  • 547
Sascha
  • 302
  • 5
  • 11

1 Answers1

0

You can use sed like this:

cat fooBar
The quick brown fox jumps over </dog>

myVar='the lazy'
sed -i.bak "s~</dog>~$myVar &~" fooBar

cat fooBar
The quick brown fox jumps over the lazy </dog>
anubhava
  • 664,788
  • 59
  • 469
  • 547