- Your Own Linux.
- Linux How To’s | Bash Scripting | Python
- Sunday, 19 April 2015
- Sed Command in Linux — Append and Insert Lines to a File
- sed — Appending Lines to a File
- 1. Append a line after ‘N’th line
- 2. Append Line using Regular Expression/Pattern
- sed — Inserting Lines in a File
- 1. Insert line using the Line number
- 2. Insert lines using Regular expression
Your Own Linux.
Linux How To’s | Bash Scripting | Python
Sunday, 19 April 2015
Sed Command in Linux — Append and Insert Lines to a File
This is the second article of the «Super sed ‘ Series», in which we will learn how to append and insert lines to a file using line numbers and regular expressions. In the previous article in the series, we learned to print lines in a file using sed command.
Before we directly jump to the main content, every learner should know what sed is. Here is the brief introduction of the Super sed :
- sed stand for Stream EDitor and it being based on the ed editor, it borrows most of the commands from the ed . It was developed by Lee E. McMahon of Bell Labs.
- sed offers large range of text transformations that include printing lines, deleting lines, editing line in-place, search and replace, appending and inserting lines, etc.
- sed is useful whenever you need to perform common editing operations on multiple lines without using ‘vi’ editor.
- Whenever sed is executed on an input file or on the contents from stdin, sed reads the file line-by-line and after removing the trailing newline, places it in the «Pattern space», where the commands are executed on them after conditions (as in case of regex matching) are verified, and then printed on the stdout.
sed — Appending Lines to a File
For our better understanding, let us have a file sedtest.txt with contents as follows:
1. Append a line after ‘N’th line
This will add a line after ‘N’th line in the FILE.txt .
Example:
To append a line #This is just a commented line after 1st line,
While, to append a line after last line,
If you run above commands and inspect the file sedtest.txt , you would find that, the original contents of that file would not change. In case you wish to append lines in the file and save the changes (i.e. edit the file in place), you will have to use the option -i .
Lets check it for the latest command we have run to append lines after the last line of the file. Has it made any changes to the file?
No, the original file remains the same. But, I wanted to save the changes to the file. So, I should have used the option -i .
Yes, now changes are written to the file. Just remember this.
2. Append Line using Regular Expression/Pattern
This will append the line after the line where pattern match is found.
sed — Inserting Lines in a File
1. Insert line using the Line number
This will insert the line before the line at line number ‘N’.
While, to insert a line before last line,
2. Insert lines using Regular expression
This will insert the line before every line where pattern match is found.
That’s all about the second article on sed command. More articles on sed are coming soon. So, stay tuned. Of course, do not forget to share your feedback in the comment section below.
Источник