Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The sed FAQ
Prev Home Next

4.40. How do I read (insert/add) a file at the top of a textfile?

Normally, adding a "header" file to the top of a "body" file is done from the command prompt before passing the file on to sed. (MS-DOS below version 6.0 must use COPY and DEL instead of MOVE in the following example.)

       copy header.txt+body temp                  # MS-DOS command 1
       echo Y | move temp body                    # MS-DOS command 2
                                                    #
       cat header.txt body >temp; mv temp body    # Unix commands

However, if inserting the file must occur within sed, there is a way. The sed command "1 r header.txt" will not work; it will print line 1 and then insert "header.txt" between lines 1 and 2. The following script solves this problem; however, there must be at least 2 lines in the target file for the script to work properly.

     # sed script to insert "header.txt" above the first line
     1{h; r header.txt
       D; }
     2{x; G; }
     #---end of sed script---
The sed FAQ
Prev Home Next

 
 
   Reprinted courtesy of Eric Pement. Also available at https://sed.sourceforge.net/sedfaq.html Design by Interspire