Reg python regexp

Chris Angelico rosuav at gmail.com
Wed Mar 21 06:56:43 EDT 2018


On Wed, Mar 21, 2018 at 9:44 PM,  <sankarramanv at gmail.com> wrote:
> Hi,
>
> I have a requirement.
>
> cmd="cat <file>|grep -c 'if [ -t 1 ]; then mesg n 2>/dev/null; fi'"
>
> I need to escape only the square brackets in above variable since its not grepping without escaping the brackets.
>
> Please help.

You're putting this into a Python script. Why not use Python to search
the file instead of grep? That'd also eliminate the superfluous "cat
file |" at the start.

Python is not a shell language. You don't have to, and shouldn't,
write everything by invoking other programs.

ChrisA



More information about the Python-list mailing list