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
  • 713,503
  • 59
  • 514
  • 593
Sascha
  • 336
  • 5
  • 12

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
  • 713,503
  • 59
  • 514
  • 593