How to Strip Dollar Signs ($) from Data/ Escape Special Characters in R

Removing Two Characters From A String

Since answering in the comments is bad:

gsub('\\$|,', '', d)

replaces either $ or (|) , with an empty string.

Write to text file but escape special characters

I also played around with this once. If I remember correctly:

  • the double quotes you need to escape with \
  • if you want to write a \ you also need to escape it with \
  • if you want to write a \" you need to \\"


taskFilename = "example.txt"

cat("
#!/usr/bin/perl
use strict;
use warnings;

use File::Find;
use File::Temp qw(tempfile);

my @imagedir_roots = (\"/Users/Ross/Desktop/images\");

my $parallel = 8;

my $exiftool_command = 'exiftool -all= -tagsfromfile @ -all:all --gps:all --xmp:geotag -unsafe -icc_profile -overwrite_original';

# Create the (temporary) -@ files
my @atfiles;
my @atfilenames;
for (my $i = 0; $i < $parallel; ++$i) {
my ($fh, $filename) = tempfile(UNLINK => 1);
push @atfiles, $fh;
push @atfilenames, $filename;
}

# Gather all JPG image files and distribute them over the -@ files
my $nr = 0;
find(sub { print { $atfiles[$nr++ % $parallel] } \"$File::Find::name\n\" if (-f && /\\.(?:jpg|jpeg)/i); }, @imagedir_roots);

# Process all images in parallel
printf(\"Processing %d JPG files...\n\", $nr);
for (my $i = 0; $i < $parallel; ++$i) {
close($atfiles[$i]);
my $pid = fork();
if (!$pid) {
# Run exiftool in the background
system qq{$exiftool_command -@ \\\"$atfilenames[$i]\\\"};
last;
}
}

# Wait for processes to finish
while (wait() != -1) {}

", fill = TRUE, file = taskFilename
)

How to delete \ characters in R string?

It is the double quote that is creating the issue. We can use gsub

gsub('"', '', string)

Write to text file but escape special characters

I also played around with this once. If I remember correctly:

  • the double quotes you need to escape with \
  • if you want to write a \ you also need to escape it with \
  • if you want to write a \" you need to \\"


taskFilename = "example.txt"

cat("
#!/usr/bin/perl
use strict;
use warnings;

use File::Find;
use File::Temp qw(tempfile);

my @imagedir_roots = (\"/Users/Ross/Desktop/images\");

my $parallel = 8;

my $exiftool_command = 'exiftool -all= -tagsfromfile @ -all:all --gps:all --xmp:geotag -unsafe -icc_profile -overwrite_original';

# Create the (temporary) -@ files
my @atfiles;
my @atfilenames;
for (my $i = 0; $i < $parallel; ++$i) {
my ($fh, $filename) = tempfile(UNLINK => 1);
push @atfiles, $fh;
push @atfilenames, $filename;
}

# Gather all JPG image files and distribute them over the -@ files
my $nr = 0;
find(sub { print { $atfiles[$nr++ % $parallel] } \"$File::Find::name\n\" if (-f && /\\.(?:jpg|jpeg)/i); }, @imagedir_roots);

# Process all images in parallel
printf(\"Processing %d JPG files...\n\", $nr);
for (my $i = 0; $i < $parallel; ++$i) {
close($atfiles[$i]);
my $pid = fork();
if (!$pid) {
# Run exiftool in the background
system qq{$exiftool_command -@ \\\"$atfilenames[$i]\\\"};
last;
}
}

# Wait for processes to finish
while (wait() != -1) {}

", fill = TRUE, file = taskFilename
)

How to search a dollar sign in a string in R?

Since dollar sign is a special character you have to escape it by preceding a backslash like this:

\$

You can check the documentation

The fundamental building blocks are the regular expressions that match a single character. Most characters, including all letters and digits, are regular expressions that match themselves. Any metacharacter with special meaning may be quoted by preceding it with a backslash. The metacharacters in extended regular expressions are . \ | ( ) [ { ^ $ * + ?, but note that whether these have a special meaning depends on the context.

Update: as Gregor pointed in his comment, you have to escape the backslash in R, so you have to use:

\\$

You could have:

grep("\\$[^0-9]", cand_emp$description, ignore.case = TRUE, perl = FALSE, value = FALSE,
fixed = FALSE, useBytes = FALSE, invert = FALSE)

On the other hand, if you don't like having the two backslashes, you can leverage the character class using [``] by having:

grep("[$][^0-9]", cand_emp$description, ignore.case = TRUE, perl = FALSE, value = FALSE,
fixed = FALSE, useBytes = FALSE, invert = FALSE)

Update: I noticed that you updated your question with I want to flag all the strings which have a dollar sign followed by a number. For this you have to remove the ^ from your example, use:

[$][0-9]+


Related Topics



Leave a reply



Submit