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?
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"
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.
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.
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
Or we can just use '"'
on the pattern
gsub('"', "", data)
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With