data in a file
Let's say file1.txt contain the following text.
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
The following sed statement can be used to print the every line in file1.txt that contains "Line 1". Typically, you would use grep for this kind of filtering.
~]# sed -n '/Line 1/ p' file1.txt
Line 1
The following sed statement will print only "line 1" and "line 3".
~]# sed -n '/Line 1/ p; /Line 3/ p' file1.txt
Line 1
Line 3
The following sed statement can be used to print the text between Line 3 and Line 5.
~]# sed -n '/Line 3/, /Line 5/ p' file1.txt
Line 3
Line 4
Line 5
The following sed statement will print only line 1 and everything from line 3 through line 5.
~]# sed -n '/Line 1/ p; /Line 3/, /Line 5/ p' file1.txt
Line 1
Line 3
Line 4
Line 5
The following sed statement can be used to delete the text between Line 2 and Line 4.
~]# sed -i '/Line 1/, /Line 3/ d' file1.txt
Line 1
Line 5
data not in a file
This can also be used against data not in a file, such as data in a variable or array. Let's say $foo contains the following.
Line 1
Line 2
Line 3
Line 4
Line 5
The following sed statement can be used to print the every line in $foo t that contains "Line 1". Typically, you would use grep for this kind of filtering.
~]# echo $foo | sed -n '/Line 1/ p'
Line 1
The following sed statement can be used to print the text between Line 2 and Line 4.
~]# echo $foo | sed -n '/Line 2/, /Line 4/ p'
Line 2
Line 3
Line 4
The following sed statement can be used to delete the text between Line 2 and Line 4.
~]# echo $foo | sed '/Line 1/, /Line 3/ d'
Line 1
Line 5