adding a block of text before a line in a file

I would like to add a block of text before a specific content inside a file using bash.

Example, I have the content file with the following block of text
AliasMatch 1
AliasMatch 2

I have the master file with the following content

beginning of line
end of line1
end of line2

I want to add the block of text before "end of line1". So I want the resulting master file to be like

beginning of line
AliasMatch 1
AliasMatch 2
end of line1
end of line2

How can I do that ?
LVL 49
sunray_2003Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vikas_madhusudanaCommented:
#! /bin/sh

cat file.txt > temp.txt
cat master.txt >> temp.txt
rm master.txt
mv temp.txt master.txt

Try this before trying take a backup of master file as this script will delete the file and then create it.
0
sunray_2003Author Commented:
I want to add
AliasMatch 1
AliasMatch 2

between the lines

beginning of line
end of line1
0
vikas_madhusudanaCommented:
ok

head -n 1 master.txt > temp.txt
cat file.txt > >temp.txt
tail -n +2 master.txt  >> temp.txt
rm master.txt
mv temp.txt master.txt

here file.txt will have the contents
AliasMatch 1
AliasMatch 2


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

tty2Commented:
Before the line "end of line1":

sed -e "s/^end of line1$/AliasMatch 1\nAliasMatch 2\nend of line1/"  master.file

After the line "beginning of line":

sed -e "s/^beginning of line$/beginning of line\nAliasMatch 1\nAliasMatch 2/" master.file
0
tty2Commented:
In addition: insert the text exactly between two matched lines.

sed -e '/^beginning of line$/{n;s/^end of line1$/AliasMatch 1\nAliasMatch 2\nend of line1/};' master.file

If you need to change the source file 'master.file', add parameter -i

sed -i -e '/^beginning of line$/{n;s/^end of line1$/AliasMatch 1\nAliasMatch 2\nend of line1/};' master.file
0
DileepanCommented:
use the script below and pass the following as parameters

<script> <master.file> '<line to search>' <insert.file>

#!/bin/bash

mainfile=$1
line=$2
infile=$3
count=`wc -l $masterfile | cut -d ' ' -f 1`
i=1

while [ $count -ge $i ]
do
   curline=`head -$i $mainfile | tail -1`
   if [ "$curline" = "$line" ]
   then
          cat $infile
   fi
   echo $curline
   i=`expr $i + 1`
done
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.