1

i want a python equivalent of shell command cat logs/archived/app.2020-12-11* | grep "Sending" > sms_otp_logs_ack.log

I have tried by using sh module.

sh.grep(sh.cat(2020-12-18/100.6.13.16/app.*), string_to_grep)

This throws an error of no such file due to * wild card. How can i pypass this and also save the output to another file?

3
  • Did you try the python module os? check os.system() and use the same shell command as argument to os.system() Commented Dec 18, 2020 at 9:11
  • The cat is mildly useless here; like basically every properly written file manipulation tool, grep accepts an arbitrary number of file name arguments, with the marked benefit that it can print which file it found a match in if you would like that. Commented Dec 18, 2020 at 10:59
  • can you provide me the edited shell command to grep from all those files @tr Commented Dec 18, 2020 at 11:16

1 Answer 1

2

You can use glob module which finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, and then those found files are opened up in read mode to be searched for the desired string("Sending") in order the lines containing that string are to be appended to another file.

import glob, os

for f in glob.glob(os.path.join(path, "app.2020-12-11*")):
    with open(f, 'r') as f_in, open('sms_otp_logs_ack.log', 'a') as f_out:
        for line in f_in:
            if "Sending" in line:
                f_out.write(line)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.