写入文本文件但转义特殊字符

时间:2017-02-01 14:13:04

标签: r

我正在尝试使用R将一些perl脚本写入文本文件。我只是无法弄清楚如何逃避某些角色?

我使用反斜杠(单和双),方括号,“\\ Q ... \\ E”等,但仍然无法使其正常工作。

任何帮助将不胜感激。提前致谢!

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
)

1 个答案:

答案 0 :(得分:1)

我也玩过一次。如果我没记错的话:

  • 您需要使用\
  • 转义的双引号
  • 如果你想写一个\你还需要用\
  • 来逃避它
  • 如果你想写一个\&#34;你需要\\&#34;

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
)

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 )