I need to find exact string from a file. Assume simple text file contains 3 lines of text
SILVERGOLD-A
SILVERGOLD-AMY
SILVERGOLD-ACB
and I need to find exact string "SILVERGOLD-A"
. I am using following command in the terminal:
cat text.txt | grep "\bSILVERGOLD-A\b"
and I am able to successfully get only "SILVERGOLD-A"
as output. However, it is not working in Python with subprocess.POpen
. My code looks like:
cmd1 = ['cat', text.txt]
cmd2 = ['grep', find_string] ==> Where String is find_string = '\'\b' + find_string + '\'\b'
ps = subprocess.Popen(cmd1, stdout=subprocess.PIPE)
grep = subprocess.Popen(cmd2, stdin=ps.stdout, stdout=subprocess.PIPE)
ps.stdout.close()
print("grepout {0}".format(grep.communicate()))
But I continue to get empty grep results. Any clue or help?
And if I remove \b
from string then I do get all string matching (even partial).