Ross
Ross

Reputation: 359

Write to text file but escape special characters

I'm trying to use R to write some perl script to a text file. I just cannot figure out how to escape certain characters?

I've used backslash (single and double), square brackets, "\\Q...\\E", etc. but still can't make it work.

Any assistance would be appreciated. Thanks in advance!

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
)

Upvotes: 0

Views: 586

Answers (1)

ricoderks
ricoderks

Reputation: 1619

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
)

Upvotes: 1

Related Questions