I have a text file that contains lots of text however, I only want specific lines/characters printed out. How can do I this?
Example Text-file
edit 29
set status enable
set sender-pattern-type default
set sender-pattern *
set recipient-pattern-type default
set recipient-pattern *
set sender-ip-type ip-group
set sender-ip-group
set reverse-dns-pattern *
set reverse-dns-pattern-regexp no
set authenticated any
unset tls-profile
set action relay
set comment
I want to print only lines that contain edit
, set action
and set comment
.
How I want it
edit 29
set action relay xyz
set comment xyz
edit 30
set action relay abc
set comment acb
etc.
Current code:
# Save results of command in "Output" while splitting one whole output line into multiple lines (original output).
output = "".join(ssh_stdout.readlines())
# Save SSH output to a text-file.
file = open('outputFortimail.txt', "w")
file.write(output)
file.close()
file.write("\n".join(
line for line in file.split("\n") if any(line.startswith(x)
for x in
["edit:", "set sender-ip-mask:", "set comment:", "unset comment"])))
file.close()
Split on newline, filter, and join back on newline:
"\n".join(
line for line in contents.split("\n") if any(line.startswith(x) for x in ["Name:", "Type:", "Area:"])
)
Where your file is in the string contents
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.