Operating System - HP-UX
1833156 Members
3181 Online
110051 Solutions
New Discussion

Re: insert text into a file after a particular line

 
SOLVED
Go to solution
Anand_30
Regular Advisor

insert text into a file after a particular line

Hi,

I have a file which contains around 8 lines of text. I have to insert the contents of this file into another file after say line number 79.

Can anyone please help me out in this.

Thanks,
Andy

11 REPLIES 11
Elena Leontieva
Esteemed Contributor

Re: insert text into a file after a particular line

Andy,

Do vi of your second file, put cursor at line 79 and type ":r filename" where filename is the name of your 8-lines file.

Elena.
Anand_30
Regular Advisor

Re: insert text into a file after a particular line

Thanks Elena,

I know how to do it in vi. But I have to do it in a SHELL script.

Thanks,
Andy

curt larson_1
Honored Contributor

Re: insert text into a file after a particular line


lineNum=79
rfile=File2Insert
file=File2AddText

ex -s +"$lineNum|r $file|wq" file
curt larson_1
Honored Contributor

Re: insert text into a file after a particular line

or

file1=file2insert
file2=text2insert
file3=tmpfile

lineNum=79

sed -ne '1,$lineNum' -e '$lineNum r $file2' -e '$((lineNum+1)),$p' file 1 > file3
mv file3 file1
curt larson_1
Honored Contributor

Re: insert text into a file after a particular line

oops forgot a p for print and need to add a space so shell doesn't take the $p as a variable

sed -ne '1,$lineNum' -e '$lineNum r $file2' -e '$((lineNum+1)),$p' file 1 > file3

should be

sed -ne "1,$lineNum p" -e "$lineNum r $file2" -e "$((lineNum+1)),$ p" file1 > file3
Hein van den Heuvel
Honored Contributor

Re: insert text into a file after a particular line

And if you for soem reason prefer ed:

echo "r \nw\nq\n" | ed

Or Perl (in case you want to do something fancy in determining that line number from STDIN:

#!/bin/perl
$line = 0 + shift @ARGV;
$file = shift @ARGV || die "Need line-number, File-name to insert";
open (FILE,"<$file") || die "failed to open insert file $file";
while (<>) {
print;
print if (++$i == $line);
}

Or Perl from file name on argument list:

#!/bin/perl
$line = 0 + shift @ARGV;
$file = shift @ARGV;
$orig = shift @ARGV || die "Need line-number, File-name to insert, File";
open (ORIG,"<$orig") || die "failed to open original $file";
open (FILE,"<$file") || die "failed to open insert file $file";
while () {
print;
print if (++$i == $line);
}


cheers,
hein.
Anand_30
Regular Advisor

Re: insert text into a file after a particular line

Hi Curt,

I tried using your sed solution but it does not work. It is not writing anything to the tmpfile.

Can you please look into it.

Thanks,
Andy.
Hein van den Heuvel
Honored Contributor
Solution

Re: insert text into a file after a particular line

very simple working SED example with variables:

> cat x1
aap
noot
mies
teun
> cat x2
x
x
> export LINE=2
> export FILE=x2
> sed "${LINE}r $FILE" x1
aap
noot
x
x
mies
teun


of course ou need to escape the LINE variable name to make sure the 'r' for read is not eaten up with up.


hth,
Hein.
Anand_30
Regular Advisor

Re: insert text into a file after a particular line

Thanks Hein,

It works absolutely fine now.

Andy

KapilRaj
Honored Contributor

Re: insert text into a file after a particular line

file1 --- The file to be added
file2 --- The file on which file1 needs to be added.
LEN=`wc -l file2`
LINENUM=`echo "LINE -1"|bc`
cat file2 |head -$LINENUM >>/tmp/file_new
cat file1 >>/tmp/file_new
cat file2 |tail -`echo "$LEN - 78" |bc` >> /tmp/file_new

A simple way of doing it as i do not know much about sed and awk !

Kaps
Nothing is impossible
Elmar P. Kolkman
Honored Contributor

Re: insert text into a file after a particular line

If you want to do it that way, you could also do it without temporary files:

file1 --- The file to be added
file2 --- The file on which file1 needs to be added.
file3 --- Resulting file
line --- line where file1 needs to be inserted

LEN=`wc -l <$file2` # to prevent the filename in the output, use redirection
PART1=`expr $line - 1`
PART2=`expr $LEN - $PART1`
LINENUM=`echo "LINE -1"|bc`
(
head -$PART1 $file2
cat $file1
tail -$PART2 $file2
) > $file3
Every problem has at least one solution. Only some solutions are harder to find.