-1

How to rename file name from Pbs_d7_s2_juliam_08July2020_02_1_0_live singlets.fcs to Pbs_d7_s2.fcs

For multiple files keeping in mind that _juliam_08July2020_02_1_0_live singlets is not the same for all files?

manotheshark
  • 4,297
  • 17
  • 30
  • 2
    Have you tried anything? Are counted characters alpha only or alpha numeric? Are underscores and the extension excluded from the character count? The provided example has five alpha characters and seven alpha numeric characters (excluding underscores and extension). – manotheshark Aug 06 '20 at 12:14

1 Answers1

0

It's a bit unclear what you're asking for, but it looks like you only want to keep the chunks before the third underscore. If so, you can tackle this with regular expressions. The regular expression

    str_extract(input_string, "^(([^_])+_){3}")

will take out the first 3 blocks of characters (that aren't underscores) that end in underscores. The first ^ "anchors" the match to the beginning of the string, the "[^_]+_" matches any number of non-underscore characters before an underscore. The {3} does the preceding operation 3 times.

So for "Pbs_d7_s2_juliam_08July2020_02_1_0_live singlets.fcs" you'll end up with "Pbs_d7_s2_". Now you just replace the last underscore with ".fcs" like so

    str_replace(modified string, "_$", ".fcs")

The $ "anchors" the characters that precede it to the end of the string so in this case it's replacing the last underscore. The full sequence is

    string1<- "Pbs_d7_s2_juliam_08July2020_02_1_0_live singlets.fcs" 
    str_extract(string1, "^(([^_])+_){3}") %>%
    str_replace("_$",".fcs")

    [1] "Pbs_d7_s2.fcs"

Now let's assume your filenames are in a vector named stringvec.

    output <- vector("character",length(stringvec))
    for (i in seq_along(stringvec)) {
    output[[i]] <- str_extract(stringvec[[i]],"^(([^_])+_){3}")%>% 
    str_replace("_$",".fcs")
     }
     output

I'm making some assumptions here - namely that the naming convention is the same for all of your files. If that's not true you'll need to find ways to modify the regex search pattern.

I recommend this answer How do I rename files using R? for replacing vectors of file names. If you have a vector of original file names you can use my for loop to generate a vector of new names, and then you can use the information in the link to replace one with the other. Perhaps there are other solutions not involving for loops.

RStudious
  • 166
  • 5
  • I should have specified that you need the stringr package for the functions I used and magrittr package for the %>% symbol in the for loop. So you can add library(stringr) and library(magrittr) to the top, or better yet library(tidyverse) – RStudious Aug 06 '20 at 17:19