473,408 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Read Character by Character in each line

I'm trying to read each line, character by character, to determine if a line has a semicolon in it. I'm trying to count the number of lines that have a semicolon. In all of the files in one directory.

I have 2 problems, one problem is it displays '.' and '..' and the subdirectory as files which I want it to avoid. I tried to use grep but I was unsuccessful. The second problem I'm having is that while it will read the amount of lines I have, I want it to only count the lines that have a semicolon in it. My approach to this problem was to read each line character by character and compare it to semicolon, if it matched than the line_count would increment by 1 otherwise goto next line.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/bin/perl
  3.  
  4. use strict;
  5. #use warnings;
  6.  
  7. use File::Copy;
  8.  
  9. my $line_count = 0;
  10. my $dir_name = "C:\\Example\\";
  11.  
  12. opendir DIR, $dir_name or die "Error opening dir\n";
  13.  
  14. #for each file in the directory, display the name and count the number of lines
  15. foreach my $file (readdir DIR)
  16. {
  17.     print "File in $dir_name is $file \n";
  18.  
  19.     #need to put dir_name and file next to each other because
  20.     #they are both strings which combined together locate the file.
  21.     open (MYFILE, "$dir_name$file");
  22.     #while we haven't gotten to the EOF, count the lines
  23.     while (<MYFILE>) 
  24.     {
  25.         #my @lines = <MYFILE>;
  26.  
  27.         chomp;
  28.         #if(@lines eq ';')
  29.         #{
  30.             $line_count++;
  31.         #}
  32.  
  33.         print "$line_count";
  34.         print "$_\n";
  35.     }
  36.     close (MYFILE);
  37.  
  38.     print "There are $line_count lines in $file\n\n";
  39.  
  40.     $line_count = 0;
  41. }
  42.  
  43. closedir DIR;
  44.  
  45.  
In the meantime, I will continue to figure this out, if I do I will post the solution.
Thanks in advance for any help.
Mar 19 '08 #1
6 4494
d41
2
'.' and '..' are two folders (I think) in every directory. '.' leads into the current directory, and '..' leads into the parent directory. If you're reading all the files in a foreach loop, you could just tell it next; if the filename is '.' or '..'. Or you could have it skip all filenames that start with a dot '.'.

For checking if a line has a semicolon, you can just use a regular expression or the index() function
regular expression: /;/ (Don't use a regular expression for something this simple, though)
or the index function: index($line,";"); will return the place it found a semicolon, or -1 if it didn't find a semicolon.
Mar 19 '08 #2
numberwhun
3,509 Expert Mod 2GB
I would actually go a different direction with this. Taking into account that you want to not have the "." and "..", you would need to figure out how to do an "ls" of whatever directory it is and then scan each line (I would personally use a regular expression) for the semi colon.

Here is some code I put together a little while ago that does an "ls -rt" of a directory, only using strictly perl, no system() function.

Expand|Select|Wrap|Line Numbers
  1. ### Declarations ###
  2. my @filesarray;
  3. my $directory;
  4.  
  5. ### Test to ensure that there is only one directory supplied on the command line
  6. if($#ARGV != "0")
  7. {
  8.    print("This script requires you to supply one and only one direcotry path.\n");
  9.    print("Please re-execute this script with a directory path.\n");
  10.    print("Usage:  lsrt.pl <directory_path>\n");
  11.    exit();
  12. }
  13.  
  14. ### Take the argument supplied on the command line and put it into a variable
  15. $directory = shift @ARGV;
  16.  
  17. ### Change directories to the directory specified as an option to this script
  18. chdir($directory);
  19.  
  20. ### Take the files in the current directory (obtained through the glob <*>)
  21. ### and compare them, sorting them into the array by the oldest first.
  22. ### The variables $a and $b are used specifically by sort and if you
  23. ### reverse them here, you will get the sort in the reverse order with the
  24. ### newest as the first element of the array.
  25. @filesarray = sort{ -M $b <=> -M $a } <*>;
  26.  
  27. ### Print out the array, one element per line.
  28. foreach(@filesarray)
  29. {
  30.     print("$_\n");
  31. }
  32.  
There should be plenty of comments there to enable you to understand it.

After you incorporate the bit that does the listing into your script, you will then have to examine each line (again, regex for that) for the ";".

But that is just another idea of what you can do. How you do it is up to you, I just wanted to share. (TIMTOWTDI)

Regards,

Jeff
Mar 19 '08 #3
Thanks a lot guys, I'm going to try both approaches to see what happens. d41 thanks for the filename comparison idea and your approach to checking semicolons. numberwhun your code makes it easier to see the bigger picture.
Mar 19 '08 #4
KevinADC
4,059 Expert 2GB
I would avoid doing this as a general rule:

Expand|Select|Wrap|Line Numbers
  1. @filesarray = sort{ -M $b <=> -M $a } <*>;
sorting files that way is slow and in this case there appears to be no reason to sort the files at all. The better way if you did have to sort the files would be to get all the -M values first then sort them. That way you never have to stat the same file more than once.
Mar 19 '08 #5
Ok thanks to your help guys, I found the solution. Here it is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/bin/perl
  3.  
  4. use strict;
  5. #use warnings;
  6.  
  7. my $line_count = 0;
  8. my $dir_name = "C:\\Example\\";
  9.  
  10. opendir DIR, $dir_name or die "Error opening dir\n";
  11.  
  12. #for each file in the directory, display the name and count the number of lines
  13. foreach my $file (readdir DIR)
  14. {
  15.     #every directory has '.' and '..' as folders, skip over them
  16.     if($file eq '.')
  17.     {
  18.         next;
  19.     }
  20.     elsif($file eq '..')
  21.     {
  22.         next;
  23.     }
  24.  
  25.     print "File in $dir_name is $file \n";
  26.  
  27.     #need to put dir_name and file next to each other because
  28.     #they are both strings which combined together locate the file.
  29.     open (MYFILE, "$dir_name$file");
  30.     #while we haven't gotten to the EOF, count the lines
  31.     while (<MYFILE>) 
  32.     {
  33.         chomp;
  34.  
  35.         my $result = index($_, ";");
  36.  
  37.         if($result != -1)
  38.         {
  39.             $line_count++;
  40.         }    
  41.  
  42.         #print "\nresult: $result\n"
  43.         print "$line_count";
  44.         print "$_\n";
  45.  
  46.     }
  47.     close (MYFILE);
  48.  
  49.     print "There are $line_count lines in $file\n\n";
  50.  
  51.     $line_count = 0;
  52. }
  53.  
  54. closedir DIR;
  55.  
  56.  
Mar 19 '08 #6
KevinADC
4,059 Expert 2GB
Nothing wrong with your code, this is just less verbose:

Expand|Select|Wrap|Line Numbers
  1. #!/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. my $dir_name = "C:\\Example\\";
  6. opendir DIR, $dir_name or die "Error opening dir: $!\n";
  7. readdir DIR; #skip '.'
  8. readdir DIR; #skip '..'
  9.  
  10. while (my $file = readdir DIR){
  11.    my $line_count = 0;
  12.    print "File in $dir_name is $file \n";
  13.    open (MYFILE, "$dir_name$file") or print "Could not open $file: $!\n";
  14.    #while we haven't gotten to the EOF, count the lines
  15.    while (<MYFILE>){
  16.       chomp;
  17.       $line_count++ if ( index($_, ';') > -1);
  18.       print "$line_count\n";
  19.    }
  20.    close (MYFILE);
  21.    print "There are $line_count lines in $file\n\n";
  22. }
  23. closedir DIR;
Mar 19 '08 #7

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

Similar topics

18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
2
by: CJ | last post by:
Newbie here. Trying to tackle following: Read text file containing list of files delimited by end of line chars. I need to somehow write a loop that filters out each of the lines of text...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
6
by: placid | last post by:
Hi all, I have been looking into non-blocking read (readline) operations on PIPES on windows XP and there seems to be no way of doing this. Ive read that you could use a Thread to read from the...
2
by: Rajen | last post by:
Suppose the field length is 25 characters. After entering the 25th character, it should be available to process. Program should not wait for the user to press enter/return key. Thank you.
7
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following...
16
by: mazwolfe | last post by:
Someone recently asked about reading lines. I had this code written some time ago (part of a BASIC-style interpreter based on H. Shildts in Art of C) to read a file with the lines ended in any...
6
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.