Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 144
  • Last Modified:

Leaving of lines

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
WesleyWee
Asked:
WesleyWee
1 Solution
 
zerofillCommented:
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
 
ozoCommented:
while( <> ){
    $count += length if 10..0;
}
print $count;
0
 
wilcoxonCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
WesleyWeeAuthor Commented:
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
 
zerofillCommented:
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
 
WesleyWeeAuthor Commented:
#!/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
 
WesleyWeeAuthor Commented:
i'm sorry i didn't paste the code that i used with your code.
0
 
zerofillCommented:
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
 
WesleyWeeAuthor Commented:
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
 
zerofillCommented:
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
 
WesleyWeeAuthor Commented:
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
 
zerofillCommented:
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
 
WesleyWeeAuthor Commented:
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
 
zerofillCommented:
unless ($line = /^.+L1\.UMC$/){next;}
0
 
WesleyWeeAuthor Commented:
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
 
zerofillCommented:
My mistake
unless ($_ = /^.+\.UMC$/){next;} #processes all *.UMC
if ($line = /^.+L1\.UMC$/){next;} #ignores all *L1.UMC
0
 
WesleyWeeAuthor Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now