Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
File1 Contents:

line1-file1      "1" 
line2-file1      "2"
line3-file1      "3" 
line4-file1      "4" 

File2 Contents:

line1-file2     "25"  
line2-file2     "24"  
Pointer-file2   "23"  
line4-file2     "22" 
line5-file2     "21"

After the execution of perl/shell script,

File 2 content should become

line1-file2     "25"  
line2-file2     "24" 
line1-file1      "1" 
line2-file1      "2"
line3-file1      "3" 
line4-file1      "4" 
Pointer-file2   "23" 
line4-file2     "22" 
line5-file2     "21"

i.e Paste the contents of file 1 in file 2 before that "Pointer" containing line.

Thanks

share|improve this question
1  
Also asked at StackOverflow –  glenn jackman Feb 28 '12 at 11:37

4 Answers 4

up vote 8 down vote accepted

sed has a function for that, and can do the modification inline:

sed -i -e '/Pointer/r file1' file2

But this puts your Pointer line above the file1. To put it below, delay line output:

sed -n -i -e '/Pointer/r file1' -e 1x -e '2,${x;p}' -e '${x;p}' file2 
share|improve this answer

Without using sed or awk...

First, find your the line on which is your pattern:

line=$(grep -n 'Pointer' file2 | cut -d ":" -f 1)

Then, use 3 commands to output the wanted result:

{ head -n $(($line-1)) file2; cat file1; tail -n +$line file2; } > new_file

This has the drawback of accessing 3 times the file file2, but might be clearer than a sed of awk solution.

share|improve this answer

Use a loop to read the lines in file2. If you find a line starting with Pointer, then print out file1. This is shown below:

#!/bin/bash
while IFS= read -r line
do
    if [[ "$line" =~ ^Pointer.*$ ]]
    then
        cat file1
    fi
    echo "$line"
done < file2
share|improve this answer

awk makes this fairly easy.
Insert the line before the file:

awk '/Pointer/{while(getline line<"innerfile"){print line}} //' outerfile >tmp
mv tmp outerfile

To make the inner file print after the Pointer line, just switch the order of the patterns (you need to add a semicolon to get the default action), and you can drop the line variable:

awk '//; /Pointer/{while(getline<"innerfile"){print}}' outerfile >tmp
mv tmp outerfile

And just because no one has used perl yet,

# insert file before line
perl -e 'while(<>){if($_=~/Pointer/){system("cat innerfile")};print}' outerfile

# after line
perl -e 'while(<>){print;if($_=~/Pointer/){system("cat innerfile")}}' outerfile
share|improve this answer
    
its working, but its getting removed the line containing pointer –  user1228191 Feb 28 '12 at 18:18
    
likewise, how to Paste the contents of file 1 in file 2 after that "Pointer" containing line using awk –  user1228191 Feb 28 '12 at 18:21
    
@user1228191 Fixed the first, added the second. –  Kevin Feb 28 '12 at 18:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.