English 中文(简体)
• 如何用每公升的新线取代^M
原标题:How to replace ^M with a new line in perl
  • 时间:2011-08-24 13:11:21
  •  标签:
  • perl

我的测试文件有“n”线数,在每一行之间有^M,这反过来又使它成为大举。 我的守则是公开的,应当放下头盔,然后是随后的行文,然后搜索长途和档案名称。 但是,由于档案只是一纸空洞而告终,那就没有正确开展工作。

#!/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 /	/, $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
";

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

}

一、在六国开启试验档案时,i

:%s/^M/
/g

并且删除了英国皇家骑自行车,但如何在这项 per方案内这样做? 一经试试试试,插入s^M/g,并写到不同的档案中。

最佳回答

Before you start reading the file, set $/ to " ". 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.

此外,请在

问题回答

如果为你工作,你可以写上你自己的Mac2unix,作为Perl的一班子:

perl -pi -e  tr/
/
/  file.txt

如果档案的大小大于虚拟记忆,则可能失败,因为它读到整个档案中。

为了完整起见,也谨指出:

perl -pi -e  tr/
//d  file.txt

和aix2dos:

perl -pi -e  s/
/
/g  file.txt

这一档案是否源于窗口系统? 如果是的话,在阅读之前,在档案中试运行dos2unix。 你可以在援引手稿之前或读到的文字内这样做。

贵方不妨在您的发言稿一开始将美元(投入记录号码)拨至^M,例如:

$ = "^M";

perl -MExtUtils:Command -e dos2unix





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签