Inserting text in a file

Last update on January 12, 2009 08:55 AM by jak58
Published by jak58

Inserting text in a file





Introduction


It is sometimes useful to insert text in the header of one or more document (s) or in a specific part of the document (before or after a specific line).

The publisher of flux "sed" can help us in this task and we will see how.

Syntax


Whatever the method (insertion, addition or exchange), the syntax will remains the same:

sed '{/pattern/|/regexp/|n}{i|a|c}\<text to be iserted>' file


There are three ways to insert text into a document:
  • insert beforehand (with the "i" as integration)
  • add after (with the "a" parameter as after)
  • replacement (with the "c" for change)


Insert a line with "i"
The syntax is very simple, simply designate the front line which must be inserted a new line or by its corresponding number, or a pattern.

Example:
  • With numbering

sed "16i\Notes : The denial achieved with an exclamation point! \n " file.txt
  • With parameters



sed "/Conclusion/i\Notes: negation achieved with an exclamation point ! \n" file.txt


Adding a line with "a"

The syntax is as simple, simply designate the line after which must be inserted a new line or by its corresponding number, or a pattern.

Example:
  • With numbering


sed "8a \"ed\"is a text editor making use of oriented line. It is useto create\Amend or otherwise manipulate text files."file.txt
  • With parameters


sed "/\"ed\"/a \"ed\"is a text editor making use of oriented line. It is useto create\Amend or otherwise manipulate text files."file.txt


Changing a line with "c"
The syntax remains the same, simply designate the line to be replaced by our new line, either by its corresponding number, or a pattern.

Example:
  • With numbering

sed "/14/c Various parameters can be passed at the end of expression, such as the \"g\"(for global), for a total replacement of each case"  file.txt
  • With parameters


sed "/global/c  Various parameters can be passed at the end of expression, such as the \"g\"(for global), for a total replacement of each case" file.txt
Best answers for « Inserting text in a file » in :
Writing in batch in text file Show Writing in batch in text file To write in a file text, you just have to use a redirect: echo text > output_file.txt To write in an existing file: echo " Write at the end of the file ">> output_file.txt
[MySQL]Loading a text file into ue table Show[MySQL]Loading a text file into ue table To load a text file as follows: $ tail /home/user1/test.txt 'name1',1,9 'name2',2,3 'name3',3,54 'name4',4,2 'name5',5,9 In a table defined as follows: CREATE TABLE...
Sed - inserting spaces ShowSed - inserting spaces Insert a blank line after each sentence (punctuated by a carriage return) sed G file.txt Insert a blank line after each sentence (punctuated by a carriage return), without taking into account the existing white...
Renaming multiple files in batch ShowRenaming multiple files in batch Native features Using a third-party program Renaming a large number of files can quickly become tedious. Fortunately, there are tools to automate this task in many cases: Native features On Windows...
Download Join (Merge) Text Files Software ShowDescription The application is designed by Sobolsoft. Simple and easy to use, the application has been awarded from various places. Join Text Files a tool that enables you to merge several files. It allows you to merge multiple text files into a...
File formats and extensions ShowWhat is a MIME type? The MIME type (Multipurpose Internet Mail Extensions) is a standard that was proposed by the Bell Communications laboratories in 1991 to broaden the possibilities of email, involving insertion of documents (images, sounds and...
What is a file? ShowWhat is a file? A file is a suite of binary information, i.e. a suite from 0 to 1. This file may be stored in order to keep a record of this information. A text file is a file comprising characters stored in the form of octets. This file is saved...
Managing images in HTML ShowHow do you display images on a web page? Having a few images on a website can make it more attractive and user-friendly. However, it is important not to go overboard, since images can take a long time to load, and in some cases can make a document...