0

I am trying to create a Perl script to delete lines a file using vim. I want to automate this task as I have to delete these lines over and over again. Here's the script I've written so far but it is not working.

#!/usr/bin/perl 
my $PROG        = 'hwdr';
my $PERS        = "/u/blh/scripts/70-persistent-net.rules";
open (IN, $PERS) || die "Cannot open file".$PERS."for read";
@lines = <IN>;
close IN;

open (OUT, ">", $PERS) || die "Cannot open file. Please run as root".$PERS."for write";
foreach $line (@lines)
{
    $line =~ 8,10d ;
    print OUT $line;
}

close OUT;

Thanks for any help

RobEarl
  • 7,862
  • 6
  • 35
  • 50
  • 3
    What is the error you get ? – Rndm Nov 12 '13 at 15:38
  • 5
    I know you asked for perl, but since you tagged linux, use the head command. 'head -n 10 somefile > someotherfile' – crafter Nov 12 '13 at 15:39
  • Would a Vim macro work here? – Ben Klein Nov 12 '13 at 15:42
  • Not sure if you *have to* use perl for this for some reason, but if not, this can be done in one line using head or sed. See http://stackoverflow.com/questions/13380607/how-to-use-sed-to-remove-last-n-lines-of-a-file – mti2935 Nov 12 '13 at 15:43
  • I don't get any errors. The perl script executes without any errors. The only thing it doesn't do is it doesn't edits the file. I don't want to just view it. I want to delete those lines in the file and save them. These are auto generated and I want to remove them by just running a simple script. –  Nov 12 '13 at 15:48
  • 1
    that would be `head -n 9`. or `perl -pwe'1..9 or last' somefile > someotherfile` – ysth Nov 12 '13 at 15:56
  • Can you clarify what you mean "using vim?". Do you want to be able to run a macro in VIM to delete these lines? Do you want to have Perl find these lines and delete them for a whole range of files, so you can edit them in VIM? You can define Macros in VIM using the [:map](http://vimdoc.sourceforge.net/htmldoc/map.html#key-mapping) command. If you simply want a quick way to delete these particular lines while in VIM, this is probably a better way. Please clarify your question, so I know exactly what you're looking for. – David W. Nov 12 '13 at 17:32
  • Thanks alot guys, I followed flying frog syntax and just added the move function to rename the file to the original file. Seems to work now. Thanks again :D –  Nov 13 '13 at 09:45

3 Answers3

2

Using $. to track the line number:

while (<IN>) {
    last if $. > 10;
    print;
}

Or a one line version using inplace editing:

perl -pi -e '$. > 10 && exit;' /u/blh/scripts/70-persistent-net.rules
RobEarl
  • 7,862
  • 6
  • 35
  • 50
2

Try using sed

sed -i '10,$d' file
jkshah
  • 11,387
  • 6
  • 35
  • 45
0

As an example (as you seem to want a perl script):

Input (nums.txt):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Script:

#!/usr/bin/perl -w
use strict; 

my $file = 'nums.txt';

open my $in,  '<',  $file  or die "Can't read old file: $!";
open my $out, '>', "new.$file" or die "Can't write new file: $!";

my $line_count = 0;
while( <$in> )
    {
    $line_count++;
    next if $line_count >= 10;
    print $out $_;
    }

Output (new.nums.txt):

1
2
3
4
5
6
7
8
9
fugu
  • 6,417
  • 5
  • 40
  • 75
  • that looks like it could work. But would it be possible to start deleting line number in between. lets say I want from lines 10 and below deleted? –  Nov 12 '13 at 15:52
  • Thanks I think i got really close. It is definitely working. but it seems to remove everything in the file not from certain lines. –  Nov 12 '13 at 16:00
  • All the code will do is print the entire line for lines 1 - 9 to a file named new.file. Your original file will remain the same. This is what you're after? – fugu Nov 12 '13 at 16:03
  • no not entirely. I am trying to modify the original file and not create a new.file. I want to delete and save the changes on the original file. –  Nov 12 '13 at 16:05