Home > Blockchain >  Replacing first character in line in multi-line text column
Replacing first character in line in multi-line text column

Time:11-15

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

CodePudding user response:

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"   

CodePudding user response:

You do not need any lookbehind here, use ^ with (?m) flag:

test <- gsub(test, pattern = "(?m)^o ", replacement = "• ", perl=TRUE)

The (?m) redefines the behavior of the ^ anchor that means "start of a line" if you specify the m flag.

See the online R demo:

test <- "• Direct the Department’s technical\n\no Set direction to ensure goals and objectives\n\no Select management and other key personnel"
cat(gsub(test, pattern = "(?m)^o ", replacement = "• ", perl=TRUE))

Output:

• Direct the Department’s technical

• Set direction to ensure goals and objectives

• Select management and other key personnel
  • Related