1

I'm extracting the first line that starts with 'abc' in a file

grep -w 'abc' --max-count=1 file.tsv

I wanna use it in a python program

import subprocess

process = subprocess.Popen("grep -w 'abc' --max-count=1 file.tsv",
                             shell=True,
                             stdout=subprocess.PIPE,
                           )
stdout = process.communicate()[0].split('\n')

My python is running on Windows and grep won't work. Is there an alternative that I can use in my python program.

Ank
  • 6,040
  • 22
  • 67
  • 100

2 Answers2

1

You could look into re: http://docs.python.org/2/library/re.html

Open the file and search the lines using re; this would eliminate the need to call a sub-process.

hazydev
  • 344
  • 1
  • 9
1

in Windows try this:

grep -w "abc" --max-count=1 file.tsv

grep for Windows needs "double quotes".

Endoro
  • 37,015
  • 8
  • 50
  • 63