Search code examples
rregextextgsub

Replacing first character in line in multi-line text column


I am trying to replace the "o " with "• " in this text:

• Direct the Department’s technical

• Perform supervisory and managerial responsibilities as leader of the program

o Set direction to ensure goals and objectives

o Select management and other key personnel

o Collaborate with executive colleagues to develop and execute corporate initiatives and department strategy

o Oversee the preparation and execution of department’s Annual Financial Plan and budget

o Manage merit pay

• Perform other duties as assigned

Since these are at the beginning of the line I've tried

test<- sub(test, pattern = "o ", replacement = "• ")  # does not work
test<- gsub(test, pattern = "^o ", replacement = "• ")  # does not work
test<- gsub(test, pattern = "o ", replacement = "• ") # works but it also replaces to to t•

Why does "^o " not work since it only appears at the beginning of each the line


Solution

  • Is this is all in a single value? If so, use a lookbehind to find o following either line breaks or string start:

    test2 <- gsub(test, pattern = "(?<=\n|\r|^)o ", replacement = "• ", perl = TRUE)
    cat(test2)
    
    • Direct the Department’s technical
    
    • Perform supervisory and managerial responsibilities as leader of the program
    
    • Set direction to ensure goals and objectives
    
    • Select management and other key personnel
    
    • Collaborate with executive colleagues to develop and execute corporate initiatives and department strategy
    
    • Oversee the preparation and execution of department’s Annual Financial Plan and budget
    
    • Manage merit pay
    
    • Perform other duties as assigned
    

    Alternatively, split into individual values per line, then use your original regex:

    test3 <- gsub(unlist(strsplit(test, "\n|\r")), pattern = "^o ", replacement = "• ")
    test3
    
     [1] "• Direct the Department’s technical"                                                                         
     [2] ""                                                                                                            
     [3] "• Perform supervisory and managerial responsibilities as leader of the program"                              
     [4] ""                                                                                                            
     [5] "• Set direction to ensure goals and objectives"                                                              
     [6] ""                                                                                                            
     [7] "• Select management and other key personnel"                                                                 
     [8] ""                                                                                                            
     [9] "• Collaborate with executive colleagues to develop and execute corporate initiatives and department strategy"
    [10] ""                                                                                                            
    [11] "• Oversee the preparation and execution of department’s Annual Financial Plan and budget"                    
    [12] ""                                                                                                            
    [13] "• Manage merit pay"                                                                                          
    [14] ""                                                                                                            
    [15] "• Perform other duties as assigned"