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.

I want to know how could I add files contain information under specific line in file.

For example, I have file1.txt with the following lines:

line 1
line 2
line 3

and file2.txt has the information I want to add in file1.txt under specific in line 2.

How could this to be done?

share|improve this question
add comment

2 Answers 2

Use Sed's R[ead] Command

You can use sed's r command to read in a file. For example, to match the line by its number:

sed '2 r /tmp/file2.txt' /tmp/file1.txt

Or if you prefer to match the line by its content:

sed '/^line 2/ r /tmp/file2.txt' /tmp/file1.txt

This is a standard command, and doesn't rely on any GNU extensions, so it should work across different sed implementations.

The commands above print the result on their standard output. If you want to modify the original file, add the -i option just after sed (this will work on Linux and Cygwin but not on all unix systems).

share|improve this answer
    
it just read the file1.txt but didn't add any content from file2.txt under the line 2 –  iLinux85 Sep 9 '12 at 15:56
    
and the line contains special characters like this zend_extension="/usr/local/Zend/lib/Optimizer-3.3.9/php-5.2.x/ZendOptimizer.so" when i execute the command the output => sed: -e expression #1, char 21: unknown command: `u' –  iLinux85 Sep 9 '12 at 16:01
    
@iLinux85 The original answer matched the line by its content. See my edit for how to match by line number. –  Gilles Sep 9 '12 at 21:24
add comment

Besides sed you can also do this with awk:

awk '1; /line 2/ { while(getline < "file2.txt") print }' file1.txt

This will insert the contents of file2.txt every time line 2 is encountered.

The 1 is the default block { print $0 }.

This sends its output to standard output, if you want to overwrite file1.txt save the output to a temporary file and overwrite:

awk '1; /line 2/ { while(getline < "file2.txt") print }' file1.txt > file1.txt.tmp
mv file1.txt.tmp file1.txt

Edit

For fixed string matching == would be more reliable, e.g. to match the mentioned string use this conditional:

awk '1; $0 == "\"zend_extension=/usr/local/Zend/lib/Optimizer-3.3.9/php-5.2.x/ZendOptimizer.so\"" { while(getline < "file2.txt") print }' file1.txt

Note this needs to match the whole line.

share|improve this answer
    
the output : awk: 1; /zend_extension="/usr/local/Zend/lib/Optimizer-3.3.9/php-5.2.x/ZendOptimizer.so"‌​/ { while(getline < "xh.ini") print } awk: ^ syntax error –  iLinux85 Sep 9 '12 at 17:25
    
Use == instead, see edit. –  Thor Sep 9 '12 at 18:36
    
thanks for the edit , but could u set the whole command include the edit in one line ? –  iLinux85 Sep 9 '12 at 19:02
    
Answer emended. –  Thor Sep 9 '12 at 19:38
    
i execute the command as it shows and i make sure the line same as in the file but the output shows the file1.txt like cat command didn't add any content after the line i specific –  iLinux85 Sep 9 '12 at 21:29
show 1 more comment

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.