0

I need to use the grep command to search for a pattern in a text file. But I'm on Windows so how do I do that? Oh and I have to use python.

My teacher said we have to input something like

grep "Hello" file.txt

in Python's shell.

But base on what I found in the internet, that should be inputted in Linux's terminal. I asked our teacher about it but he said I should just look for it on YouTube :(

I really don't have any idea how to do this and where to start. Please help me!

EDIT: I used re.search to do the same thing before but my teacher won't accept it. I really have to use grep!

If anyone has any idea how that is possible please tell me

Rose
  • 127
  • 7
  • See [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236) – Wiktor Stribiżew Jul 16 '20 at 10:50
  • [findstr](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/findstr) and [Select-String](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7) are comparable commands on Windows – Honinbo Shusaku Jul 16 '20 at 10:57
  • What does this have to do with Python? There's no built-in `grep` in Windows, [the closest alternative is `findstr`](https://superuser.com/questions/300815/grep-equivalent-for-windows-7). – ForceBru Jul 16 '20 at 10:57
  • @WiktorStribiżew I'm sorry. But my question is this "How do I use grep on Windows while using Python?" I want to know if this is really possible. – Rose Jul 16 '20 at 10:58
  • @ForceBru RIGHT?! But my teacher insists to use python for it. That's why I was wondering if anyone has an idea how do I do that. – Rose Jul 16 '20 at 11:00
  • @Abdul Yea, but my teacher wants grep :( – Rose Jul 16 '20 at 11:00
  • There is a Windows version of `grep`, so the answer is "yes, you may if you install all these apps". – Wiktor Stribiżew Jul 16 '20 at 11:01
  • @Rose In that case, ForceBru's link has an [answer](https://superuser.com/a/301075) that shares 2 options on getting `grep` on Windows – Honinbo Shusaku Jul 16 '20 at 11:02
  • @WiktorStribiżew I'm okay with installing apps.. but what app will make it possible for me to input/use grep in python's shell? – Rose Jul 16 '20 at 11:07
  • See https://stackoverflow.com/questions/23798617/using-grep-from-python-console – Wiktor Stribiżew Jul 16 '20 at 11:09
  • For future reference, see [What topics can I ask about here?](https://stackoverflow.com/help/on-topic), point #3: 'Questions asking for homework help' – Lucan Jul 16 '20 at 11:41
  • @Lucan Hi! Sorry I thought I already followed what it said. I'll be clearer next time. Thank you! – Rose Jul 16 '20 at 12:37

1 Answers1

1

You can use the os module.

import os

os.sytem("grep -w \"Hello\" file.txt")

# Note:
# Command has to be a string.
# Command should have escape characters, like \".

You can install grep for Windows from this site.

777moneymaker
  • 697
  • 4
  • 15
  • Hi!! I did what you said. But when I tried to print it, it only outputs "1". Why is this? – Rose Jul 16 '20 at 12:35
  • 1
    It's just and example. You should read a manual for grep and find switches that you have to use. I don't know what pattern are you looking for, so I can't help you right now. – 777moneymaker Jul 16 '20 at 12:39