How to print lines in txt from a variable and manipulate this data - Python

Asked

Viewed 59 times

-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.

  • Hey, buddy, what’s up? I’m sorry about the code, I’m a beginner user.

  • 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.

  • The first block you added, all of those are part of the text, or it was disfigured just here for us?

  • 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.

1 answer

0


I ran the test here and it’s bringing up all the data from the archive. However, the way you want to treat this data I did not apply, because I do not know in fact how you want. If it is not in the form you want to let me know that I edit or remove the answer:

with open("APIsfrontResult", "w") as api_result_write:
    with open("APIsfront.txt", "r") as api_result:
        for line in api_result:
            line = line[:-1]
            list_search = ["server_name", "proxy_pass"]
            for val_search in list_search:
                if val_search in line:
                    api_result_write.write(line + "\n")
  • Wellington, thank you so much for your help, for the first step I wanted with your example I managed to solve my initial problem!! Thanks even!

  • Regarding data processing, I am studying a way to print only the lines of the proxy_pass and the previous line, in which case it would be the server_name from a list of keywords. Could you point something out to me, please? Again, thank you so much for your help!!

  • But regarding the problem I had about server_name and proxy_pass my answer is correct? If yes, I think you should mark as answered. Because the treatment already enters as another question. No?

  • This correct friend!! I marked, sorry, my first time on the platform. It would be better to create a new question for part 2, which you suggest?

  • I think a second question would be better, so it will be available to everyone on the platform to analyse and answer. Just be careful your question isn’t a question for me. That is, try to find a model you want to follow, assemble it and if you really can’t, question the stack next to the code so they can help you. Got it?

  • Excellent, friend. It’s in the same lineage, just filtering the regex, I’m still searching for a template of the theme and trying to do. Thanks for all your help!! Big hug, my friend!!

  • Wellington, can you clear me of one last question? How is the line functioning = line[:-1], I am studying for a few days the counters, but it is still a little difficult to understand, rsrs

  • online test, to better understand I made a small example, python documentation

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.