0

I need to produce text file with specific format, It should take the input from excel/csv file that will have the thousands of rows entry in it to create text output file.

My input sample Excel/csv input is like below

enter image description here

tibble::tibble(location = c(148,85), 
               proxy_pass = c("http://110.140.110.105/", 
                              "http://110.140.110.110/"))

My output text file format is like below:

location /148/ {
            proxy_pass http://110.140.110.105/;
            proxy_set_header Authorization "xyz";
        }

location /85/ {
            proxy_pass http://110.140.110.110/;
            proxy_set_header Authorization "xyz";
        }

text after the location and proxy_pass is changing by picking values from Excel/CSV. Can I do this conversion in R? If anybody has the solution please help me out.

1 Answer 1

1

You can first create the pattern using map2 from purrr and then write the list as a text file.

list_proxy <- purrr::map2(df$location, df$proxy_pass, ~paste0("location /", .x, "/ {
            proxy_pass ", .y, ";
            proxy_set_header Authorization 'xyz';
        }"))

lapply(list_proxy, write, "proxy.txt", append=TRUE)

Output:

location /148/ {
            proxy_pass http://110.140.110.105/;
            proxy_set_header Authorization 'xyz';
        }
location /85/ {
            proxy_pass http://110.140.110.110/;
            proxy_set_header Authorization 'xyz';
        }

Data:

df <- tibble::tibble(location = c(148,85), 
               proxy_pass = c("http://110.140.110.105/", 
                              "http://110.140.110.110/"))
 
 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.