Jackie
Jackie

Reputation: 23539

Perl adding line to begininning

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.

Upvotes: 1

Views: 149

Answers (3)

Hynek -Pichi- Vychodil
Hynek -Pichi- Vychodil

Reputation: 26121

Simplest way is using sed

sed 1d

Upvotes: 1

fugu
fugu

Reputation: 6578

Or just:

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

my @file = read_file('input.txt');
shift(@file);
foreach(@file){
    ...
}

Upvotes: 1

Joseph Myers
Joseph Myers

Reputation: 6552

If indeed your problem is caused solely by the presence of one extra line at the top of your input file, and presuming you are using a typical loop like while (<FILE>) { ... }, then you can skip the first line of the input file by inserting this line at the very beginning within your loop:

next unless $. > 1;

Upvotes: 3

Related Questions