By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,692 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,692 IT Pros & Developers. It's quick & easy.

write all lines except the first one!

P: 63
i'm about to use perl to read the content of a text file then write all lines in this file, except the first line, into another text file. Reading and writing file I think is ok to me, but I don't know how to make the first line as exception :|

any help?
Sep 24 '08 #1
Share this Question
Share on Google+
8 Replies


Ganon11
Expert 2.5K+
P: 3,652
If you know how to write all lines in a loop, just read from the file once before entering the loop. Essentially, you need to get the first line, then enter into a loop that prints everything (remaining) in the file.
Sep 24 '08 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
That's what I love about Perl..... TMTOWTDI !!!

Expand|Select|Wrap|Line Numbers
  1. my $counter = 0;
  2.  
  3. open(FILE, "<file.txt") or die "$!";
  4.  
  5. LOOP: while(<FILE>) {
  6.     if($counter = 0){
  7.         $counter++;
  8.         next LOOP;
  9.     }
  10.  
  11.     .... the rest of your code...
  12. }
  13.  
Just another way of doing it using a counter. The first time through it will read a line, check the counter, which is equal to zero, iterate the counter and then grab the next line and continue processing.

Regards,

Jeff
Sep 24 '08 #3

P: 63
my bad,
just when I put the question on the forum, the answer rushed into my head:

Expand|Select|Wrap|Line Numbers
  1. open <FILE, 'text1'>;
  2. open <FILE2, 'text2'>;
  3.  
  4. $i = 0;
  5.  
  6. while (<FILE>) {
  7.    if ($i != 0) {
  8.        print FILE2 "$_\n";
  9.    }
  10.    $i++;
  11. }
  12.  
  13. close (FILE1);
  14. close(FILE2);

do u think this way is ok?

thanx so much for your help :D
Sep 24 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
my bad,
just when I put the question on the forum, the answer rushed into my head:

Expand|Select|Wrap|Line Numbers
  1. open <FILE, 'text1'>;
  2. open <FILE2, 'text2'>;
  3.  
  4. $i = 0;
  5.  
  6. while (<FILE>) {
  7.    if ($i != 0) {
  8.        print FILE2 "$_\n";
  9.    }
  10.    $i++;
  11. }
  12.  
  13. close (FILE1);
  14. close(FILE2);
  15.  
do u think this way is ok?

thanx so much for your help :D
It does look to pretty much do the same thing, yes. Nice job. All in all though, remember, Perl's motto has always been..... TMTOWTDI !!!

Nice job!

Regards,

Jeff
Sep 24 '08 #5

P: 63
a mistake, it should be
Expand|Select|Wrap|Line Numbers
  1. open(FILE2, '>text2')
so that we can write on file text2
Sep 24 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
This is much more efficient:


Expand|Select|Wrap|Line Numbers
  1. open(IN, "<file.txt") or die "$!";
  2. open (OUT, ">", 'outfile.txt') or die "$!";
  3. <IN>; #<-- discard first line
  4. print OUT <IN>;
  5. close(IN);
  6. close(OUT);
Sep 24 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
That's what I love about Perl..... TMTOWTDI !!!

Expand|Select|Wrap|Line Numbers
  1. my $counter = 0;
  2.  
  3. open(FILE, "<file.txt") or die "$!";
  4.  
  5. LOOP: while(<FILE>) {
  6.  
  7.  
  8.         $counter++;
  9.         next LOOP;
  10.     }
  11.  
  12.     .... the rest of your code...
  13. }
  14.  
Just another way of doing it using a counter. The first time through it will read a line, check the counter, which is equal to zero, iterate the counter and then grab the next line and continue processing.

Regards,

Jeff

This line is wrong:

Expand|Select|Wrap|Line Numbers
  1.     if($counter = 0){
I think you can see why Jeff.
Sep 24 '08 #8

numberwhun
Expert Mod 2.5K+
P: 3,503
This line is wrong:

Expand|Select|Wrap|Line Numbers
  1.     if($counter = 0){
I think you can see why Jeff.

He he, my quick, on the fly, untested code. Yes, it should be ==, sorry. :)
Sep 24 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.