perl 删除尾随行不起作用

标签 perl chomp chop

这是工作文件.txt

 NC_001778

NC_005252

NC_004744

NC_003096

NC_005803

我想在数组中读取它并且只有没有空格或行的字符串。 这段代码可以在我的笔记本电脑上执行我想要的操作,但它不能在 linux 桌面上运行!

  @nodes=<nodefile>;
  chomp @nodes; 

foreach my $el(@nodes){
        chop ($el);
   }
print Dumper @nodes;
#output: `bash-4.2$ perl main.pl
';AR1 = 'NC_000893
';AR2 = 'NC_001778
';AR3 = 'NC_005252
';AR4 = 'NC_004744
';AR5 = 'NC_003096
';AR6 = 'NC_005803

`

    #hexdump -C workfile.txt |head -20

00000000  4e 43 5f 30 30 30 38 39  33 0d 0d 0a 4e 43 5f 30  |NC_000893...NC_0|
00000010  30 31 37 37 38 0d 0d 0a  4e 43 5f 30 30 35 32 35  |01778...NC_00525|
00000020  32 0d 0d 0a 4e 43 5f 30  30 34 37 34 34 0d 0d 0a  |2...NC_004744...|
00000030  4e 43 5f 30 30 33 30 39  36 0d 0d 0a 4e 43 5f 30  |NC_003096...NC_0|
00000040  30 35 38 30 33 0d 0d 0a  4e 43 5f 30 30 36 35 33  |05803...NC_00653|
00000050  31 0d 0d 0a 4e 43 5f 30  30 34 34 31 37 0d 0d 0a  |1...NC_004417...|
00000060  4e 43 5f 30 31 33 36 33  33 0d 0d 0a 4e 43 5f 30  |NC_013633...NC_0|
00000070  31 33 36 31 38 0d 0d 0a  4e 43 5f 30 30 32 37 36  |13618...NC_00276|
00000080  31 0d 0d 0a 4e 43 5f 30  31 33 36 32 38 0d 0d 0a  |1...NC_013628...|
00000090  4e 43 5f 30 30 35 32 39  39 0d 0d 0a 4e 43 5f 30  |NC_005299...NC_0|
000000a0  31 33 36 30 39 0d 0d 0a  4e 43 5f 30 31 33 36 31  |13609...NC_01361|
000000b0  32 0d 0d 0a 4e 43 5f 30  30 32 36 34 36 0d 0d 0a  |2...NC_002646...|
000000c0  4e 43 5f 30 30 34 35 39  35 0d 0d 0a 4e 43 5f 30  |NC_004595...NC_0|
000000d0  30 32 37 33 34 0d 0d 0a  4e 43 5f 30 30 34 35 39  |02734...NC_00459|
000000e0  38 0d 0d 0a 4e 43 5f 30  30 34 35 39 34 0d 0d 0a  |8...NC_004594...|
000000f0  4e 43 5f 30 30 38 34 34  38 0d 0d 0a 4e 43 5f 30  |NC_008448...NC_0|
00000100  30 34 35 39 33 0d 0d 0a  4e 43 5f 30 30 32 36 34  |04593...NC_00264|
00000110  37 0d 0d 0a 4e 43 5f 30  30 32 36 37 34 0d 0d 0a  |7...NC_002674...|
00000120  4e 43 5f 30 30 33 31 36  33 0d 0d 0a 4e 43 5f 30  |NC_003163...NC_0|
00000130  30 33 31 36 34 0d 0d 0a  4e 43 5f 30 32 30 31 35  |03164...NC_02015|

有什么建议吗?提前致谢

最佳答案

问题是您在这个文件中有 Windows 行结尾,这就是为什么当您使用 linux 时,您的 chomp 没有正确删除行结尾。它没有解释为什么 chop 不删除最后一个字符,该字符应该是 chomp 之后的 \r

你的输出

';AR6 = 'NC_005803

表示字符串中的最后一个字符实际上是 \r。这不是字符串的实际问题,只是视觉表示的问题。如果你想看到这个字符按字面写出来,你可以使用选项

$Data::Dumper::Useqq = 1;

然后将产生输出

$VAR6 = "NC_005803\r";

如何解决?

一个简单的修复方法是使用 linux 中的 dos2unix 实用程序来修复该文件。要在 Perl 中修复它,您可以执行类似的操作

s/[\r\n]*\z// for @nodes;  # remove all \r and \n  from end of string
s/\s*\z// for @nodes;      # remove all whitespace from end of string
s/\r//g   for @nodes;      # remove all \r from string
tr/\r//d  for @nodes;      # same

关于perl 删除尾随行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19095398/

相关文章:

python - 命令的工作(display.chop_threshold(0))

perl - 在主目录中安装 perl 模块时出现问题

Perl+Image::Magick用法:如何将一张图片中的几个区域组合成一张新图片?

c++ - psapi.dll 进程状态api

perl - 使用 chomp() 时打印失败

python - 如何删除字符串中的前导零和尾随零? Python

c++ - Qt C++ 如何在 1 个命令中截断作为 QStringList 成员的 QString 的字母

perl - 如何从数组中删除最后一个字符?

perl - 如何使用vim在Perl中自动折叠POD?

perl - Perl 中的 "Use of uninitialized value in scalar chomp"