Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove double quote \" symbol from string

I need to remove \" from a vector. This is my data:

data <- c("\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1803224&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-linux-security-masterclass-3-in-1%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1848638&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fmastering-kali-linux%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1426684&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Finformation-gathering-with-kali-linux%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1628300&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-switchblade%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1615700&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fadministrador-de-sistemas-junior-en-windows-server-y-linux%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.809770&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flearn-bash-shell-in-linux-for-beginners-lite%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.574388&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fhow-to-install-linux-ubuntu-server%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1436610&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fcentos-and-ubuntu-managing-packages%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1771266&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-foundation-certified-system-administrator-exam%2F", 
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1734052&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-server-security%2F"
)

As you can see, every object starts with \". How can I specifically remove these characters and leave the links?

like image 458
antecessor Avatar asked Aug 15 '18 12:08

antecessor


People also ask

How do I get rid of double quotes in a string?

Remove Double Quote from a String The sed command line utility helps to easily handle this. A single line sed command can remove quotes from start and end of the string. sed -e 's/^"//' -e 's/"$//' <<<"$var1"

How do I remove a quote from a string in Python?

The first expression 's/^"//' will remove the starting quote from the string. Second expression 's/"$//' will remove the ending quote from the string. Remove Double Quote and Store Output. The result will be printed on the terminal. You can also save the result to variable and or redirect output to a file.

How to remove all quotes from a string using sed command?

The above sed command execute two expressions against the variable value. The first expression 's/^"//' will remove the starting quote from the string. Second expression 's/"$//' will remove the ending quote from the string. The result will be printed on the terminal. You can also save the result to variable and or redirect output to a file.

How to remove double quote and store output in same variable?

The below commands will help you to remove double quote and store output to the same or different variable. var2=`sed -e 's/^"//' -e 's/"$//' <<<"$var1"` #Save in another variable var1=`sed -e 's/^"//' -e 's/"$//' <<<"$var1"` #Save in same variable


3 Answers

Or we can just use '"' on the pattern

gsub('"', "", data)
like image 81
akrun Avatar answered Nov 06 '22 00:11

akrun


You can try this. Note that what you actually want is to remove \", not "\ (as proposed in the unedited version of your question). The first " you need to represent each element in the character.

gsub('[\"]', '', data)
like image 22
milan Avatar answered Nov 05 '22 23:11

milan


If it is always 1st character then just use substring:

substring(data, 2)

This should be faster than any regex solution.

data <- rep(data, 1000)

microbenchmark::microbenchmark(
  a = substring(data, 2),  
  b = gsub("\"", "", data, fixed = TRUE),
  c = gsub('"', "", data),
  d = gsub('[\"]', '', data),
  e = stringr::str_replace(data, '[\"]', ''),
  f = gsub("^.","",data)
  )
# Unit: milliseconds
# expr       min        lq      mean    median        uq       max neval
#    a  2.835013  2.849838  2.933796  2.857393  2.900301  4.446956   100
#    b  4.728632  4.739751  4.788882  4.754861  4.795203  5.200185   100
#    c  7.388025  7.413684  7.503427  7.458444  7.555520  8.160925   100
#    d  7.390876  7.412686  7.530044  7.454453  7.533568  8.535544   100
#    e 12.019154 12.205608 12.430870 12.316084 12.581081 13.917336   100
#    f 15.712882 15.735975 15.875353 15.770043 15.861275 18.906262   100
like image 38
zx8754 Avatar answered Nov 06 '22 00:11

zx8754