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
  • 761,203
  • 64
  • 569
  • 643
Sascha
  • 398
  • 6
  • 14

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
  • 761,203
  • 64
  • 569
  • 643