Monday, October 24, 2022

[SOLVED] Why is sed printing all lines?

Issue

I'm trying to learn sed. Whilst trying to learn I entered the following lines in my terminal.

~/tmp>$ cat sedtest
a
sed will enjoy writing over this
I love cats, lovely
b
sed will really really enjoy writing over this
sed will really really hate writing over this
c
~/tmp>$ sed -n '/a/,/b/p' sedtest
a
sed will enjoy writing over this
I love cats, lovely
b
sed will really really enjoy writing over this
sed will really really hate writing over this
c
~/tmp>$

Why does my sed statement print all lines? I expect it to only print in between lines that contain the letters a and b inclusive.


Solution

Change your command to

sed -n '/^a$/,/^b$/p' sedtest

in order to exclude lines containing an a or b somwhere and just match lines that are exactly a or b:

a                                                                                                                                                                                                                                         
sed will enjoy writing over this
I love cats, lovely
b       


Answered By - steffen
Answer Checked By - Mary Flores (WPSolving Volunteer)