- Thread Starter
- #26
EDIT: the question is how to remove lines containing a substring from a file using sed command. It's at the bottom of the test code sample
I think that python makes the most sense to me as postix shell probably can't easily send emails. (EDIT: previous statment does not make any sense) But most importantly I think python might come in handy in the future for AI (tensorflow and the likes) and I can easily test python code; (EDIT: Also TrueNAS, which is based on FreeBSD seems to have eather python available by default or did not remove it.)
so here goes;
I think that python makes the most sense to me as postix shell probably can't easily send emails. (EDIT: previous statment does not make any sense) But most importantly I think python might come in handy in the future for AI (tensorflow and the likes) and I can easily test python code; (EDIT: Also TrueNAS, which is based on FreeBSD seems to have eather python available by default or did not remove it.)
so here goes;
Code:
#test.py
import os;
#run a command line statement
os.system("cat /var/log/messages | grep MCA: > MCAmessages.cat"); #create new file MCAmessages.cat
## as i know how to trigger real ECC errors i can easily test this.
#already confirmed that /var/log/messages contains MCA: lines as soon as ECC errors are triggered
lineCnt = len(open("MCAmessages.cat").readlines( ));
if lineCnt > 0:
print("sending email"); #TODO: implement sending of email
else: #TODO: actually do nothing so remove the else statement
print ("no lines containing MCA: substring found in messages"); #INFO: only for testing
#remove all lines from /var/log/messages that contain MCA:
os.system("sed -i.bak '/ MCA: /' ./MCAmessages.cat"); #INFO: test it first on MCAmessages.cat
#results in;
#sed: 1 "/ MCA: /": command expected
#does anyone know the proper syntax? regex does also not seem to work
#perhaps a good resource on where the syntax is explained?