Home > database >  Why is sed printing all lines?
Why is sed printing all lines?

Time:09-20

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.

CodePudding user response:

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       

CodePudding user response:

this sed command
will begin print when it sees a line contain "a",
will stop print when it sees a line contains "b"

In your comand example it first print the flowing

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

then print

sed will really really enjoy writing over this
sed will really really hate writing over this
c

"sed will really really enjoy writing over this" this line contains "a" so it will begin print

and very important
if from the time it sees "a",it doesn't sees "b",
it will print to the end, although there is no line contains "b"

And you can try to match like this

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

which means it meets the line begins with "a" and begins with "b"

  • Related