Sed is a powerful tool to do some text manipulations in a file. We already covered some basic SED stuff.
https://www.linuxnix.com/2012/04/sedsteam-editor-explained-detail-linuxunix.html
In this post we will see one simple use case of SED. I have a requirement where I have to insert a new line with some data at a particular line. We can achieve it with 'i' operator. I have following file and I want to insert a line at 4th line as "This is my 4th line"
August 2015
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Syntax:
Example
Output:
surendra@sanne-taggle:~/code/sh$ cat ade
August 2015
Su Mo Tu We Th Fr Sa
1
This is my 4th line
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
How about appending a line to a particular line using SED?
Use "a" operator as shown below.
Example output:
surendra@sanne-taggle:~/code/sh$ cat ade
August 2015
Su Mo Tu We Th Fr Sa
1
This is my 4th line
2 3 4 5 6 7 8
This is my 6th line
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Keep visiting linuxnix.com for more how-tos.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018