1

I have a perl script I am writing to parse a file based on regex. The problem is the file always starts with a new line. When I turn on hidden chars in vi it shows up as a "$" (this is vi on cygwin). is there a way I can use regex to remove them? I tried

s/\n//g

But that did not seem to work.

4

3 回答 3

3

如果您的问题确实是由输入文件顶部的一个额外行引起的,并且假设您使用的是典型的循环while (<FILE>) { ... },那么您可以通过在最前面插入这一行来跳过输入文件的第一行从你的循环开始:

next unless $. > 1;
于 2013-09-06T19:03:04.800 回答
1

最简单的方法是使用sed

sed 1d
于 2013-09-07T00:36:14.757 回答
1

要不就:

#!/usr/bin/perl
use warnings;
use strict;
use File::Slurp;

my @file = read_file('input.txt');
shift(@file);
foreach(@file){
    ...
}
于 2013-09-06T19:09:08.243 回答