Ubuntu – Grep a line which start and end with a pre defined character

bashcommand linegrep

I am trying to fetch a line from a file file.txt which looks like this:

>This is line 1.</li>
>This is line 2.</li>
>This is line 3.</li>
>This is line 4.</li>

I need to fetch content which starts with > and ends in </li> so the output will be This is line 1. This is line 2. and so on. I have looked into this in forum but didnt found solution. This solution also didnt worked.

I ultimately have to fetch some lines from a webpage. So first I will curl webpage and then use grep command to grep that line which starts with > and ends in </li>.

Thanks.!

Best Answer

This should be enough:

grep '^>.*</li>$' input-file

The ^ and $ ensure that those parts are anchored at the start and end of the lines respectively.

You can also do:

grep -x '>.*</li>' input-file

-x looks for an exact match: the whole line should match the pattern (which implies ^ and $ is wrapped around the pattern).