-3

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).

Tonechas
  • 13,398
  • 16
  • 46
  • 80
iamMobile
  • 959
  • 2
  • 17
  • 35

1 Answers1

1

You might want to escape \ in your find_string, so that it reads '\\b'. Alternatively you can use r-string r'\b'.

Also I think you need to get rid of \' in your find_string. If it was intended to quote the regex, I guess Popen() automatically quote the args for you. The following doc seems relevant:

['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]

... arguments that need quoting or backslash escaping when used in the shell (such as filenames containing spaces or the echo command shown above) are single list elements.

So, for me cmd2 = ['grep', r'\b' + 'SILVERGOLD-A' + r'\b'] seems to work. Try this.

As a side note, grep does not need a stdin if you provide the input file; i.e., you can simplify the code as follows:

import subprocess

cmd2 = ['grep', r'\b' + 'SILVERGOLD-A' + r'\b', 'text.txt']
grep = subprocess.Popen(cmd2, stdout=subprocess.PIPE)
print("grepout {0}".format(grep.communicate()))
j1-lee
  • 13,764
  • 3
  • 14
  • 26