Home > database >  Understanding "*", ".", and "/" in sub function
Understanding "*", ".", and "/" in sub function

Time:10-31

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!

CodePudding user response:

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.

  • Related