如何用perl中的新行替换^M [英] How to replace ^M with a new line in perl

查看:36
本文介绍了如何用perl中的新行替换^M的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的测试文件有n"行,每行之间有一个 ^M,这反过来又使它成为一个大字符串.我正在使用的代码打开所述文件,应该解析出一个标题,然后是后续行,然后搜索目录路径和文件名.但是因为文件只是以一个大字符串结尾,所以它不能正常工作

My test file has "n" number of lines and between each line there is a ^M, which in turn makes it one big string. The code I am working with opens said file and should parse out a header and then the subsequent rows, then searches for the Directory Path and File name. But because the file just ends up as a big string it doesn't work correctly

#!/usr/bin/perl
#use strict;
#use warnings;

open  (DATA, "<file.txt") or die ("Unable to open file");

my $search_string = "Directory Path";
my $column_search = "Filename";
my $header =  <DATA>;
my @header_titles = split /\t/, $header;
my $extract_col = 0;
my $col_search = 0;

for my $header_line (@header_titles) {
  last if $header_line =~ m/$search_string/;
  $extract_col++;
}
for my $header_line (@header_titles) {
  last if $header_line =~m/$column_search/;
  $col_search++;
}

print "Extracting column $extract_col $search_string\n";

while ( my $row = <DATA> ) {
  last unless $row =~ /\S/;
  chomp $row;
  my @cells = split /\t/, $row;
 $cells[74]=~s/:/\//g;
$cells[$extract_col]= $cells[74] . $cells[$col_search];
print "$cells[$extract_col] \n";

}

当我在 VI 中打开测试文件时,我使用过

When i open the test file in VI i have used

:%s/^M/\r/g

这删除了 ​​^M 但我如何在这个 perl 程序中做到这一点?当我尝试一个测试程序并插入那个 s\^M/\r/g 并将它写入另一个文件时,它出现了很多汉字.

and that removes the ^M's but how do i do it inside this perl program? When i tried a test program and inserted that s\^M/\r/g and had it write to a different file it came up as a lot of Chinese characters.

推荐答案

在开始阅读文件之前,将 $/ 设置为 "\r".这默认设置为换行符,这对于 UNIX 风格的行尾来说很好,对于 DOS 风格的行尾几乎没问题,但对于你看到的旧 Mac 风格的行尾来说没用.如果你安装了 mac2unix,你也可以在你的输入文件上尝试它.

Before you start reading the file, set $/ to "\r". This is set to the linefeed character by default, which is fine for UNIX-style line endings, and almost OK for DOS-style line endings, but useless for the old Mac-style line endings you are seeing. You can also try mac2unix on your input file if you have it installed.

有关更多信息,请在 perlvar 联机帮助页 中查找INPUT_RECORD_SEPARATOR".

For more, look for "INPUT_RECORD_SEPARATOR" in the perlvar manpage.

这篇关于如何用perl中的新行替换^M的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆