Home > database >  Create text file of specific format from excel/csv input in r
Create text file of specific format from excel/csv input in r

Time:09-12

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.

CodePudding user response:

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/"))
 
 
  • Related