?
Solved

Leaving of lines

Posted on 2003-03-13
17
Medium Priority
?
142 Views
Last Modified: 2010-03-05
I have a text file. There are serveral characters. I wish to only count the characters starting from say line 10.

How do I make the code read my file from line 10 onwards only.
0
Comment
Question by:WesleyWee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
17 Comments
 
LVL 1

Expert Comment

by:zerofill
ID: 8134457
Here is your solution:


open (FILE, "file.txt");
while (<FILE>)
{
$counter++;
if ($counter<=10){next;} #breaks while cycle for first 10 lines

#this part is reached from line 11 to end of file. You have each line stored in $_
#example
$line=$_;
print "$line";

}
close (FILE);


Good luck!
0
 
LVL 84

Expert Comment

by:ozo
ID: 8134953
while( <> ){
    $count += length if 10..0;
}
print $count;
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8136478
Or, if you don't need this in a larger perl program, this will work (from the command line or in a shell script).

tail +10 | wc -c

tail +10 says to copy the contents of file starting from line 10 (to EOF) to STDOUT.  wc -c says to count the characters in the stream (from tail).
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:WesleyWee
ID: 8141481
Zerofill:
Thanks yours works, but when i run it i seem to get a problem. Its like there is a buffer problem. I have placed a sample of my files.
http://wesley.serveftp.com/temp/code.zip

Ozo:
How does it work? (keeps saying error at ){ something like that.

Wilcoxon:
I do need my first 10 lines haha but hey thanks too!
0
 
LVL 1

Expert Comment

by:zerofill
ID: 8141744
WesleyWee,
your URL doesn't work (file not found)
Be more specific describing the problem.
It must work and I think there is a simple logical error that could be fixed easily
Copy-paste part of your script here
0
 

Author Comment

by:WesleyWee
ID: 8141910
#!/usr/bin/perl

$some_dir= shift;
opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@files = grep {-f "$some_dir/$_" } readdir(DIR);
for( @files )
{
   print "\n--==$_==--\n";
   open F,"<$some_dir/$_" or warn "can't open $some_dir/$_: $!";
   $count = 0;
   while( <F> )
   {
#       print;
       $count += tr/2//;
   }
   print "number of Xs: $count\n";
}
0
 

Author Comment

by:WesleyWee
ID: 8141913
i'm sorry i didn't paste the code that i used with your code.
0
 
LVL 1

Accepted Solution

by:
zerofill earned 80 total points
ID: 8142048
I have tested you code (with my modifications I've told you in previous reply) and it works fine. There isn't buffer problem. I've tested the script on windows2000 in a folder containing more that 3000 files most of them more than 7MB. May be you have problems with configuration of perl or your operating system. The code is ok. If you are doing something more than I see in code you may need to undef some variables. This usualy causes problems when working with large number of files or cycles. But this is only supposition.
Here is the code I tested:

$some_dir= shift;
opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@files = grep {-f "$some_dir/$_" } readdir(DIR);
for( @files )
{
  print "\n--==$_==--\n";
  open F,"<$some_dir/$_" or warn "can't open $some_dir/$_: $!";
  $count = 0;
$counter=0;
while( <F> )
  {
#       print;
$counter++;
if ($counter<=10){next;}
      $count += tr/2//;
  }
  print "number of Xs: $count\n";
}

p.s. may be the problem could be in your input files (some nasty characters or something else... I can't remember at this moment)
0
 

Author Comment

by:WesleyWee
ID: 8142998
thanks alot it works very well now

erm.. say.... how do i do it with the code that i wish to check only *.ABC kind of extention files and nothing else.
0
 
LVL 1

Expert Comment

by:zerofill
ID: 8143290
Put another if (){} with regexp
Something like this:

unless ($_ = /^.+\.txt$/){next;}
#if the filename does not end with .txt the while (or any other) loop is ignored
0
 

Author Comment

by:WesleyWee
ID: 8145009
regexp?
i couldn't implement "unless ($_ = /^.+\.txt$/){next;}", i know it works cos i tried it. but of my 27 files it only processed one file.

my code is at http://www.geocities.com/wwyz83/code.zip

0
 
LVL 1

Expert Comment

by:zerofill
ID: 8145971
You must put this line after for (@files){
The regexp means:
Unless filename begin(^) with any character (.+) followed imediately by .UMC (\.UMC) at the real end of the string ($)  then do next loop of for(){}
This is quite easy regular expresion :) If you don't know how to write regexp I advice you to leran them. They are very usefull


#!/usr/bin/perl

$some_dir= shift;

if($some_dir)
{
opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@files = grep {-f "$some_dir/$_" } readdir(DIR);
for( @files )
{

unless ($line = /^.+\.UMC$/){next;}

   print "\n$_==--";
   open F,"<$some_dir/$_" or warn "can't open $some_dir/$_: $!";
   $count = 0;
   $counter = 0;
   while( <F> )
   {
     $counter++;
     if ($counter<=4){next;}
     $count += tr/2//;
   }
   print ">Number of bin2: $count\n";
}
}else{print "\n-=Please write a path name after the code=-\n\n";}
0
 

Author Comment

by:WesleyWee
ID: 8146440
what should i add so that of one of the UMC files that has a filename of ...L1.UMC i'll like my code not to process that file with that file name
0
 
LVL 1

Expert Comment

by:zerofill
ID: 8146480
unless ($line = /^.+L1\.UMC$/){next;}
0
 

Author Comment

by:WesleyWee
ID: 8146486
haha thanks haha but i need to limit only L1 so that it won't be processed. that way it'll only process L1 files isn't it?
0
 
LVL 1

Expert Comment

by:zerofill
ID: 8147371
My mistake
unless ($_ = /^.+\.UMC$/){next;} #processes all *.UMC
if ($line = /^.+L1\.UMC$/){next;} #ignores all *L1.UMC
0
 

Author Comment

by:WesleyWee
ID: 8149814
Thank you very much,...Zerofill.

jus a continuation of my coding.
http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_20553048.html

In the *L1.UMC file there are numbers(data) under the colmume Bin2. I need to extract the data. How do i go about it?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question