Search code examples
rtext

Change a specif character/string to double space in *txt file


I'l like to change a specif character/string to double space in *txt file. In my case each "x" must be double space.

In my example I have:

my_text <- readLines("https://raw.githubusercontent.com/Leprechault/trash/main/test.txt")
my_text
 [1] "\"x\""                                                        
 [2] "\"Relatòrio de desempenho dos diferentes produtos testados\"" 
 [3] "\"x\""                                                        
 [4] "\"Vittia P&D\""                                               
 [5] "\"x\""                                                        
 [6] "\"Data:2022-12-27\""                                          
 [7] "\"x\""                                                        
 [8] "\"Período de tempo analisado\""                               
 [9] "\"x\""                                                        
[10] "24"                                                           
[11] "\"Df\" \"Deviance\" \"Resid. Df\" \"Resid. Dev\" \"Pr(>Chi)\""

And my desirable output must be:

my_text_new <- readLines("test_new.txt")
my_text_new
 [1] ""                                                             
 [2] ""                                                             
 [3] "\"Relatòrio de desempenho dos diferentes produtos testados\"" 
 [4] ""                                                             
 [5] ""                                                                                                                         
 [6] "\"Vittia P&D\""                                               
 [7] ""                                                             
 [8] ""                                                                                                                         
 [9] "\"Data:2022-12-27\""                                          
[10] ""                                                             
[11] ""                                                                                                                        
[12] "\"Período de tempo analisado\""                               
[13] ""                                                             
[14] ""                                                                                                                       
[15] "24"                                                           
[16] ""                                                             
[17] ""                                                             
[18] "\"Df\" \"Deviance\" \"Resid. Df\" \"Resid. Dev\" \"Pr(>Chi)\""

Please, any help with it?

Thank in advance!


Solution

  • my_text <- readLines("https://raw.githubusercontent.com/Leprechault/trash/main/test.txt")
    
    my_text <- my_text[rep(seq(my_text), (as.integer(my_text == "\"x\"")+1))]
    my_text[which(my_text == "\"x\"")] <- ""
    
    head(my_text)
    #> [1] ""                                                               
    #> [2] ""                                                               
    #> [3] "\"Relat\xf2rio de desempenho dos diferentes produtos testados\""
    #> [4] ""                                                               
    #> [5] ""                                                               
    #> [6] "\"Vittia P&D\""
    

    p.d. excuse me by unicode escapes i have no portuguese config in this pc