I have a sample file with below content at /tmp/file
This is line one
This is line two
This is line three
This is line four
Here in we have to put our content “your text” at
- one line before the string is matched
- one line after the string is matched
Example 1
Here I want to put my content before “This is line two”
Solution
# sed ‘/This is line two/iyour text’ /tmp/file
This is line one
your text
This is line two
This is line three
This is line four
To perform in place operation
# sed -i ‘/This is line two/iyour text’ /tmp/file
Example 2
Here I want to put my content after “This is line two”
Solution
# sed ‘/This is line two/ayour text’ /tmp/file
This is line one
This is line two
your text
This is line three
This is line four
To perform in place operation
# sed -i ‘/This is line two/ayour text’ /tmp/file