1

Im trying to understand what the syntax is for a sub() function. The code is this:

path = "/Volumes/Elements/GTEx-v8_WGCNA/Annotations/signed/Adipose-Subcutaneous_0.8125Pathway_Enrichment_all_results.xls"
name = sub(".*/", "", path)
name = sub("Pathway_Enrichment_all_results.xls.*", "", name)

I'm just confused as to what

name = sub(".*/", "", path)

is doing.

Thanks!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
cgill22
  • 15
  • 2
  • 1
    For regex, https://stackoverflow.com/a/22944075/3358272 is a good reference, and I occasionally use https://regexr.com/, https://regex101.com/ as well. – r2evans Oct 30 '22 at 21:04

1 Answers1

0

sub() takes 3 arguments:

  1. The pattern you are looking to match
  2. The replacement string
  3. The input string

In your case, a regex is being passed as the first param. It finds any amount of characters up until the last forward slash in the path and replaces it with an empty string.

It finds and removes /Volumes/Elements/GTEx-v8_WGCNA/Annotations/signed/ and you are left with dipose-Subcutaneous_0.8125Pathway_Enrichment_all_results.xls.

Then it removes Pathway_Enrichment_all_results.xls and you are left with dipose-Subcutaneous_0.8125.

E Joseph
  • 316
  • 2
  • 8