Reputation: 307
I used grep command from shell and it gave the result I wanted but when I run from my python script using os.popen it said
grep: SUMMARY:: No such file or directory
Normal grep command:
grep -A 12 -i "LOGBOOK SUMMARY:" my_folder/logbook.log
Python script
command="grep -A 12 -i LOGBOOK SUMMARY: my_folder/logbook.log"
result=os.popen(command)
Normal grep command gave the result I wanted. 2nd one said no such file or directory
Upvotes: 2
Views: 3359
Reputation: 124704
You need to enclose the search pattern within quotes:
command="grep -A 12 -i 'LOGBOOK SUMMARY:' my_folder/logbook.log"
How to diagnose such problems? Start from the error message:
grep: SUMMARY:: No such file or directory
This error message tells that grep
could not find a file named SUMMARY:
.
The right question to ask is, why is grep
looking for a file named SUMMARY:
?
And the answer is that on the command line you executed,
somehow SUMMARY:
is considered a filename:
command="grep -A 12 -i LOGBOOK SUMMARY: my_folder/logbook.log"
Of course! That's what would happen if you executed that command in the shell:
grep -A 12 -i LOGBOOK SUMMARY: my_folder/logbook.log
Here, the shell will split the command line on spaces,
and pass to grep
3 arguments, LOGBOOK
, SUMMARY:
and my_folder/logbook.log
.
The first argument, LOGBOOK
is used as the pattern to search for,
and all remaining arguments are taken as filenames to search in.
Upvotes: 4