Remove surrounding text using Python - python

I have created a Python script that replaces text and adds quotes to characters from a text file. I would like to remove any other surrounding lines of text, which usually starts with the word "set".
Here is my current code:
import re
with open("SanitizedFinal_E4300.txt", "rt") as fin:
with open("output6.txt", "wt") as fout:
for line in fin:
line = line.replace('set system host-name EX4300', 'hostname "EX4300"')
line = line.replace('set interfaces ge-0/0/0 unit 0 family inet address', 'ip address')
line = re.sub(r'set interfaces ge-0/0/0 description (.*)', r'interface 1/1\nname "\1"', line)
line = re.sub(r'set interfaces ge-0/0/1 description (.*)', r'interface 1/2\nname "\1"', line)
#and so on...
fout.write(line)
The source text file contains surrounding text like this:
set system auto-snapshot
set system domain-name EX4300.lab
set system time-zone America/New_York
set system no-redirects
set system internet-options icmpv4-rate-limit packet-rate 2000
set system authentication-order tacplus
set system ports console type vt100
I would like to remove any other text that I have not called for in the code.
I have tried adding this to the bottom of my code with no success:
for aline in fin:
new_data = aline
if new_data.startswith("set"):
new_data = ""

What I would do is read the file, create a string with all of the info, then write it to a different file. It would go something like this:
import re
with open("SanitizedFinal_E4300.txt", "r") as f:
read = f.read()
info = ""
for line in read.split("\n"):
og_line = line
line = line.replace('set system host-name EX4300', 'hostname "EX4300"')
line = line.replace('set interfaces ge-0/0/0 unit 0 family inet address', 'ip address')
line = re.sub(r'set interfaces ge-0/0/0 description (.*)', r'interface 1/1\nname "\1"', line)
line = re.sub(r'set interfaces ge-0/0/1 description (.*)', r'interface 1/2\nname "\1"', line)
if "set" in line and line == og_line: # line == og_line makes sure that "line" wasn't changed at all
line = ""
if line != "":
info += line + "\n"
with open("output6.txt", "w+") as f:
f.write(info)

Related

Python File Opening with a variable

I am a beginner in python . why the variable "env" being used while opening the file and other places in the following code . From the tutorials i have seen file opening formats were always like with open(filename) as f , but here a variable name is being used. Please explain .
def main():
global debug, testing,env, mode
newID=0
rv=[]
name=''
if "debug" in sys.argv:
debug = True
if "testing" in sys.argv:
testing = True
if debug:
print(sys.argv)
if "copyAccountProfile" in sys.argv:
mode = 'copyAccountProfile'
envProfile = sys.argv[1]
if debug:
print(envProfile)
with open(envProfile) as f_in:
lineList = list(line for line in (l.strip() for l in f_in) if line)
# ff = open(envProfile)
# lineList = [line for line in ff.readlines() if line.strip()]
for line in lineList:
if line.startswith('#'):
continue
[nm, v]=line.strip().split('=')
env[nm.strip()]=v.strip()
if debug:
print(env)
with open(env['accountProfile']) as f_in:
accountList = list(line for line in (l.strip() for l in f_in) if line)
if mode == 'copyData':
for acc in accountList:
[name,Id] = acc.strip().split(',')
name = name.strip()
#copyData(name, env['src_ror'], workingDir, env['dst_ror'], workingDir, 'bk_files', True)
copyData(name, env['src_ror'], workingDir, env['dst_ror'], workingDir, 'bk_files')
copyData(name, env['src_ror'], workingDir, env['dst_bpm'], backupDir, 'bk_files')
copyData(name, env['src_bpm'], workingDir, env['dst_bpm'], workingDir, 'ft_files')
localCopyData(name, env['dst_bpm'], workingDir, backupDir)
else:
#copy updated accountProfile from dst_ror to dst_bpm
fname = env['accountProfile']
# src = ip:port, dst = ip:port}
[sIp, sPort]=env['dst_ror'].split(':')
[dIp, dPort]=env['dst_bpm'].split(':')
copyFile(fname, sIp, sPort, toolDir, dPort, dIp, toolDir)
Welcome Prema. Variable names can be anything the author wishes them to be. Generally, a programmer should use descriptive variable names that relate to the data being stored in that variable.
Since this example is poorly documented, I'll have to assume that the author used "env" to show that this variable is somehow connected to an environment of some sort.
This author seems to be using the "f_in" variable name to indicate the file is an input file.

Python to add a line after the exact match from the blocks of same pattern in a file

I have a file like this :
tablename-0-username=
tablename-0-password=
tablename-0-endpoint=
tablename-1-username=
tablename-1-password=
tablename-1-endpoint=
In python how to add a line after tablename-username
The keywords are like tablename-0-id=test and tablename-1-id=test1
Is there anyway in python I can create a outfile like this
tablename-0-username=
tablename-0-id=test
tablename-0-password=
tablename-0-endpoint=
tablename-1-username=
tablename-1-id=test1
tablename-1-password=
tablename-1-endpoint=
I don't want 2 files to be create, the operation should be in same file. I have created like this, but this one inserts after every other matching pattern.
find = "tablename-" + id
with open(config_file_name, "r") as in_file:
buf = in_file.readlines()
with open(config_file_name, "w") as out_file:
for line in buf:
if find in line:
line = line + pattern + "\n"
out_file.write(line)
Your problem is that, for some reason, you chose to search for a dysfunctional pattern. You have to search for something that accurately discriminates the lines yof interest from everything else. Instead, try
find = "-username="
... which uniquely identifies the lines, just as you specified in the problem description.
You can try :
find = "-endpoint"
with open(config_file_name, "r") as in_file:
buf = in_file.readlines()
with open(config_file_name, "w") as out_file:
for line in buf:
if find in line:
line = line + pattern + "\n"
out_file.write(line)
This will insert a new line (\n character) after every occurrence of the word endpoint
with open(config_file_name, "r") as in_file:
buf = in_file.readlines()
with open(config_file_name, "w") as out_file:
for line in buf:
line = line.split('=')[0]
if 'username' in line :
# You can also write a newline into the file in this block.
line = line + pattern + "\n"
out_file.write(line)
This will check for the phrase username in each line before '=' as the username can be the string "username".

What is the correct way to list a specific string in txt file then place it in a csv file? [python]

I have this sample_vsdt.txt file containing SHA-1 and description like this inside of my txt file:
Scanning samples_extracted\02b809d4edee752d9286677ea30e8a76114aa324->(Microsoft RTF 6008-0)
->Found Virus [Possible_SCRDL]
Scanning samples_extracted\0349e0101d8458b6d05860fbee2b4a6d7fa2038d->(Adobe Portable Document Format(PDF) 6015-0)
->Found Virus [TROJ_FRS.VSN11I18]
Example:
SHA-1: 02b809d4edee752d9286677ea30e8a76114aa324
Description:(Microsoft RTF 6008-0)
Problem:
My task is to list those SHA-1 and Description in my txt file then list it in a csv file, I was able to do that using regex,prefix and delimeter. However this example is what makes it hard for me:
Scanning samples_extracted\0191a23ee122bdb0c69008971e365ec530bf03f5
- Invoice_No_94497.doc->Found Virus [Trojan.4FEC5F36]->(MIME 6010-0)
- Found 1/3 Viruses in samples_extracted\0191a23ee122bdb0c69008971e365ec530bf03f5
It has different line pattern and I only want to get the SHA-1 in the first line not the 4th line and get the description in the second line.
Output:
The output went wrong because the description (MIME 6010-0) was put in the SHA-1 column.
0191a23ee122bdb0c69008971e365ec530bf03f5
(MIME 6010-0)
02b809d4edee752d9286677ea30e8a76114aa324 (Microsoft RTF 6008-0)
0349e0101d8458b6d05860fbee2b4a6d7fa2038d (Adobe Portable Document Format(PDF) 6015-0)
035a7afca8b72cf1c05f6062814836ee31091559 (Adobe Portable Document Format(PDF) 6015-0)
Code
import csv
import re
INPUTFILE = 'samples_vsdt.txt'
OUTPUTFILE = 'output.csv'
PREFIX = '\\'
DELIMITER = '->'
DELIMITER2 = ']->'
PREFIX2 = ' - '
def read_text_file(inputfile):
data = []
with open(inputfile, 'r') as f:
lines = f.readlines()
for line in lines:
line = line.rstrip('\n')
if re.search(r'[a-zA-Z0-9]{40}', line) and not "Found" in line: # <----
line = line.split(PREFIX, 1)[-1]
parts = line.split(DELIMITER)
data.append(parts)
else:
if "->(" in line and "Found" in line :
matched_words=(re.search(r'\(.*?\)',line))
sha =(re.search(r'[a-zA-Z0-9]{40}',line))
if matched_words!=None:
matched_words=matched_words.group()
matched_words=matched_words.split("]->")
data.append(matched_words)
#data.append(parts)
return data
def write_csv_file(data, outputfile):
with open(outputfile, 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"')
for row in data:
csvwriter.writerow(row)
def main():
data = read_text_file(INPUTFILE)
write_csv_file(data, OUTPUTFILE)
if __name__ == '__main__':
main()
Here is the full content of my text file:
sample_vsdt.txt
I changed some logic, maybe I can give you some different ideas.
Basically it checks if the string Scanning samples_extracted is present with (, which means that the description is on the same line of the sha.
Otherwise with only Scanning samples_extracted means that the desc is on the following line ( in your example there are some blank line, I had to add a while cycle )
Prints the result, cherry-pick logic and put in your program.
import re
with open("vCjjGQxe.txt") as f:
for line in f:
if "Scanning samples_extracted" in line and "(" in line:
sha = re.search('\\\(.*)->', line).group(1)
desc = re.search('->\((.*)\)', line).group(1)
print("SHA-1:", sha)
print("Description:", desc)
continue
if "Scanning samples_extracted" in line:
sha = re.search('\\\(.*)$', line).group(1)
while True:
i = next(f)
if "(" in i:
desc = re.search('->\((.*)\)', i).group(1)
break
print("SHA-1:", sha)
print("Description:", desc)

How to write the data from two variable into a File python

I have a log file where i'm fetching only the desired output and into two different variables and when i run the code it prints currently but i need both the variable data written into another file called file1.
I have the raw code sample which i tried working with print but not getting the idea about writing it into a file.
with open("testfile","r") as fh:
for line in fh:
if "ping" in line:
if HOST != "NA" and Flag:
mydata1 = hostname
elif HOST != "NA" and Flag and HOST not in mydata1:
mydata2 = logname
mydata3 = open('file1', 'w')
mydata3.write(mydata1,mydata2)
mydata3.close()
#print(mydata1,mydata2)
Try using str.format:
Ex:
with open("testfile","r") as fh:
for line in fh:
if "ping" in line:
if HOST != "NA" and Flag:
mydata1 = hostname
elif HOST != "NA" and Flag == True and HOST not in mydata1:
mydata2 = logname
mydata3 = open('file1', 'w')
mydata3.write("{0} {1}".format(mydata1,mydata2))
mydata3.close()

Replace lines in file from For Loop

Let's say that I have a file that contains different MAC address notations of multiple MAC addresses. I want to replace all the matching notations of one MAC address I have parsed from an argument input. So far my script generates all the notations I need and can loop through the lines of the text and show the lines that have to be changed.
import argparse, sys
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--filename")
parser.add_argument("-m", "--mac_address")
args = parser.parse_args()
mac = args.mac_address #In this case 00:1a:e8:31:71:7f
colon2 = mac #00:1a:e8:31:71:7f
dot2 = colon2.replace(":",".") # 00.1a.e8.31.71.7f
hyphen2 = colon2.replace(":","-") # 00-1a-e8-31-71-7f
nosymbol = colon2.replace(":","") # 001ae831717f
colon4 = ':'.join(nosymbol[i:i+4] for i in range(0, len(nosymbol), 4)) # 001a:e831:717f
dot4 = colon4.replace(":",".") # 001a.e831.717f
hyphen4 = colon4.replace(":","-") # 001a-e831-717f
replacethis = [colon2,dot2,hyphen2,dot4,colon4,nosymbol,hyphen4]
with open(args.filename, 'r+') as f:
text = f.read()
for line in text.split('\n'):
for n in replacethis:
if line.replace(n, mac) != line:
print line + '\n has to change to: \n'line.replace(n,mac)
else:
continue
If the file would look like this:
fb:76:03:f0:67:01
fb.76.03.f0.67.01
fb-76-03-f0-67-01
001a:e831:727f
001ae831727f
fb76.03f0.6701
001ae831727f
fb76:03f0:6701
001a.e831.727f
fb76-03f0-6701
fb7603f06701
it should change to:
fb:76:03:f0:67:01
fb.76.03.f0.67.01
fb-76-03-f0-67-01
00:1a:e8:31:71:7f
00:1a:e8:31:71:7f
fb76.03f0.6701
00:1a:e8:31:71:7f
fb76:03f0:6701
00:1a:e8:31:71:7f
fb76-03f0-6701
fb7603f06701
I am struggling at writing the new lines containing the changed MAC address notation back to the file replacing the previous line.
Is there a way to do this?
A simple way to achieve what you are asking you can add a line to store the final values you get, and after that include another ‘with open’ statement to write it to a new file.
replacethis = [colon2, dot2, hyphen2, dot4, colon4, nosymbol, hyphen4]
final_values =[]
with open(args.filename, 'r+') as f:
text = f.read()
for line in text.split('\n'):
for n in replacethis:
if line.replace(n, mac) != line:
print line + '\n has to change to: \n'line.replace(n,mac)
final_values.append(line.replace(n, mac)
else:
continue
final_values.append(line)
with open(new_file_name, ‘w’) as new_f:
new_f.write(final_values)
Note that if new_file_name = your old file name, you will overwrite the original file.
I hope that answers your question.

Categories