-1
Please, friends, I am working on a python script that reads a txt file and from two values that exist in some lines, it prints in a new txt file. In addition, the results need to stay together, that is, if these two variables are 6 distinct lines, for me it makes no sense the result.
proxy_set_header X-Samesite-Support "$xSamesiteSupport";\
proxy_pass
\
map $http_cookie $interceptError \{\
proxy_pass
~*(force_error=1) false;\
default true;\
\}\
\
server \{\
listen 8080;\
listen 8443 ssl;\
proxy_connect_timeout 1000ms;\
server_name ~^sample.(exemplo|nginx)\\..*$;\
\
location /exemplo/exemplo/sample \{\
proxy_pass http://exemplo;\
\}\
\
location /nginx/monitoring/sample \{\
return 200;\
\}\
\
\
\}\
\
# --- exemplo -----\
\
server \{\
listen 8080;\
listen 8443 ssl;\
server_name exemplo.com;\
\
\
location / \{\
client_max_body_size 2000m;\
proxy_pass http://exemplo.com; \
\}\
\
\}\
\
In this example above, you would need to print only the two server_name and proxy_pass fields one below the other in a txt or xls, one next to the other.
would look that way, taking out the garbage from fields that don’t have together:
-------------------------------------------
server_name ~^sample.(exemplo|nginx)\\..*$;\
proxy_pass http://exemplo;\
-------------------------------------------
server_name exemplo.com;\
proxy_pass http://exemplo.com; \
-------------------------------------------
What I’ve managed to develop so far: I can only print one of the texts, not in a different way:
in_file = "/Users/usuario1/Desktop/Consult/APIsfront.txt"
out_file = "/Users/usuario1/Desktop/results.txt"
search_for = "server_name"
search_for2 = "proxy_pass"
line_num = 0
line_num2 = 0
lines_found = 0
with open(out_file, 'w') as out_f:
with open(in_file, "r") as in_f:
for line in in_f:
line_num += 1
if search_for in line:
lines_found += 1
with open(in_file, "r") as in_f2:
for line in in_f2:
line_num2 +=1
if search_for2 in line:
lines_found +=1
print("Found '{}' and {} in line {}... ".format(search_for, search_for2, line_num))
out_f.write(line)
print("Found {} lines...".format(lines_found))
A colleague suggested me to use the counter and giving an append in the closed file, but I didn’t understand very well how the counter works for this example.
Does anyone have any suggestions?
The problem was not very clear, mainly because it is not defined the rule that the code tries to apply in the formation of the result.
– RHER WOLF
Hey, buddy, what’s up? I’m sorry about the code, I’m a beginner user.
– MarioRenan
But the goal is basically to print only the lines that contain proxy_pass and server_name, one below the other, because they are unique information for me, whether printing in an xls or txt.
– MarioRenan
The first block you added, all of those are part of the text, or it was disfigured just here for us?
– Wellington Fonseca
They are that way, totally non-standard document, where I need to take the server_name and proxy_pass Infos and print in a txt or xls.
– MarioRenan