1

I have a list of file names

fastq/SRR17405580 
fastq/SRR17405578 
fastq/SRR17405579

I want to change their name to look like

SRR17405580 
SRR17405578 
SRR17405579
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
  • Does this answer your question? [How do I specify a dynamic position for the start of substring?](https://stackoverflow.com/questions/3003527/how-do-i-specify-a-dynamic-position-for-the-start-of-substring) – user438383 Aug 30 '22 at 10:30

4 Answers4

2

You could use basename() to remove all of the path up to and including the last path separator.

x <- c("fastq/SRR17405580", "fastq/SRR17405578", "fastq/SRR17405579")

basename(x)

# [1] "SRR17405580" "SRR17405578" "SRR17405579"

You could also use regex:

sub('.+/(.+)', '\\1', x)

# [1] "SRR17405580" "SRR17405578" "SRR17405579"
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
1

Another option is to use word from stringr package:

library(stringr)
word(x, 2, sep="/")

[1] "SRR17405580" "SRR17405578" "SRR17405579"
TarJae
  • 72,363
  • 6
  • 19
  • 66
1

Another option using str_split from stringr like this:

library(stringr)
str_split(x, '/', simplify = TRUE)[,2]
#> [1] "SRR17405580" "SRR17405578" "SRR17405579"

Created on 2022-08-27 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53
1

Using trimws from base R

trimws(x, whitespace = ".*/")
[1] "SRR17405580" "SRR17405578" "SRR17405579"
akrun
  • 874,273
  • 37
  • 540
  • 662