Issue
Given the input
From fairest creatures we desire the increase,
That The reby the rose might never die,
Placeholders don't exist
But as tHe riper should by time decease,
I'd like to match the lines with case-insensitive variations of the
, so it should also obtain the line with The reby
and tHe riper
above. I want to use sed
to achieve this. I tried the command
sed -n "/\bthe\b/ip"
But it printed
p
p
p
If I try sed -n "/\bthe\b/pi"
it prints
From fairest creatures we desire the increase,
That The reby the rose might never die,
But as tHe riper should by time decease,
So it's close, but it has the empty line. How can I remove it and get only the lines I want?
Solution
I think you want this pattern: sed -n '/\bthe\b/Ip'
which uses an upper-case i instead of a lower-case one. I've tested it using GNU sed and it seemed to work.
The documentation is partially ambiguous when it lists the possible flags (https://www.gnu.org/software/sed/manual/sed.html#index-Case_002dinsensitive-matching):
I
i
The I modifier to regular-expression matching is a GNU extension which makes sed match regexp in a case-insensitive manner.
It becomes a little more precise later on, though:
/regexp/I
\%regexp%I
The I modifier to regular-expression matching is a GNU extension which causes the regexp to be matched in a case-insensitive manner.
In many other programming languages, a lower case i is used for case-insensitive regular expression matching. However, in sed the i is used for the insert command (see insert command).
Answered By - IonuČ› G. Stan Answer Checked By - Marilyn (WPSolving Volunteer)