Perl script that counts lines of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script that counts lines of a file
# 1  
Old 06-02-2009
Perl script that counts lines of a file

I am working on this script, but hit a bump. Looking for a little help figuring out the last part:

Code:
open(MY_FILE, $ARGV[0]) or die

$COUNTER = 1;
$LINE = <FILE>;
while ($LINE, <FILE>) {
# Adds leading zeros for numbers 1 digit long
   if ($COUNTER<10){
      print "000";
   }
   # Adds leading zeros for numbers 2 digits long
   if (($COUNTER>9) && ($COUNTER<100)){
      print "00";
   }
   # Adds leading zeros for numbers 3 digits long
   if (($COUNTER>99) && ($COUNTER<1000)){
   print "0";
   }
   # Prints line number and line
   print "$COUNTER: $LINE \n";
   $COUNTER += 1;
    }
    close FILE;

script counts the lines of a file, and outputs each line along with its line number

example: 0001:#first line of the file

script counts the lines in the file, but only outputs the first line of the file on every line. Not sure what I am missing, I know it has something to do with what I assign to the $LINE variable, and the operator in the while loop, but I am stuck. I hate asking for help, but I am banging my head on the keyboard.
# 2  
Old 06-03-2009
this:

Code:
while ($LINE, <FILE>) {

should be:

Code:
while ($LINE = <FILE>) {

You don't need a counter, perl counts the lines already and stores the current line number of the current open file in the $. (dollar-sign dot) variable. And if you are using @ARGV to read the file you don't even need to use open(). And you can use printf to format the ouput. so your code can be reduced to this succint yet clear code:


Code:
while (<>) { #<- opens @ARGV auto-magically
   printf "%04d: %s\n",$.,$_;
}

# 3  
Old 06-03-2009
take a look at perldoc -q count for example of counting lines in file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counts not matching in file

I can not figure out why there are 56,548 unique entries in test.bed. However, perl and awk see only 56,543 and that # is what my analysis see's as well. What happened to the 5 missing? Thank you :). The file is attached as well. cmccabe@DTV-A5211QLM:~/Desktop/NGS/bed/bedtools$wc -l... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

UNIX/Perl script to call informatica source counts

Hi Guys, I am trying below condition . We are using Informatica 9.5 and scheduling certain informatica mapping on set timings .But we are not sure whether the database source table are latest or not .Since its gets updated on daily basis and not sure when it completes.Can we write any unix/perl... (1 Reply)
Discussion started by: Perlbaby
1 Replies

3. Shell Programming and Scripting

Filtering lines for column elements based on corresponding counts in another column

Hi, I have a file like this ACC 2 2 21 aaa AC 443 3 22 aaa GCT 76 1 33 xxx TCG 34 2 33 aaa ACGT 33 1 22 ggg TTC 99 3 44 wee CCA 33 2 33 ggg AAC 1 3 55 ddd TTG 10 1 22 ddd TTGC 98 3 22 ddd GCT 23 1 21 sds GTC 23 4 32 sds ACGT 32 2 33 vvv CGT 11 2 33 eee CCC 87 2 44... (1 Reply)
Discussion started by: polsum
1 Replies

4. Shell Programming and Scripting

perl script print the lines between two pattern

i have a file as below sample.pl parameter1 argument1 argument2 parameter2 I want out as below argument1 argument2 that is , i want to print all the lines between parameter1 & parameter 2. i tried with the following if($mystring =~ m/parameter1(.*?)parameter2/) (2 Replies)
Discussion started by: roopa
2 Replies

5. Shell Programming and Scripting

Script to compare table counts...

Hello all, I am very new to unix. I am working on a DB upgrade. The requirement is to get count of all the tables in the database before and after upgrade and compare them. I found the below query to find the counts of all the tables: nawk -v v="'" '{ print("select " v ":" $1":" v... (2 Replies)
Discussion started by: vkrisaak
2 Replies

6. Shell Programming and Scripting

Perl - need script for modify lines

Hello, does somebody can make script for me, which replace: ąćę ąćę ąćę (input file) to ace;|;ąćę ace;|;ąćę ace;|;ąćę (output file) (3 Replies)
Discussion started by: Xadrian
3 Replies

7. Shell Programming and Scripting

how to use IF-THEN within csh script to check Db counts

hi everyone, rookie scripter here. Under some time pressure here and hoping you can help. I've written a .csh script that needs to check a row count in an Oracle table, then do an IF-THEN check against that count. Two different select statements actually, one after another. If either of the two... (1 Reply)
Discussion started by: gregrobinsonhd
1 Replies

8. UNIX for Dummies Questions & Answers

Perl/shell script count the lines

Hi Guys, I want to write a perl/shell script do parse the following file input file content NPA-NXX SC 2084549 45 2084552 45 2084563 2007 2084572 45 2084580 45 3278411 45 3278430 45 3278493 530 3278507 530... (3 Replies)
Discussion started by: pistachio
3 Replies

9. Shell Programming and Scripting

Perl script to scan back lines

Hi Perl gurus, I have this file to scan through. Sample lines below: 2008031A, USERNAME, 12345, give ABC, take XYZ, transaction submitted 2008031B, USERNAME, 12346, waiting for processing 2008031C, USERNAME, 12347, Retrieving response 2008031D, USERNAME, 12348, This is not a valid dealing... (3 Replies)
Discussion started by: gholdbhurg
3 Replies

10. Solaris

file size counts??

Hello experts, I do - $ ls -lhtr logs2007* Is it possible that i can get the results of- totals size in MB/KB for ALL "logs2007*" note: in the same directory I have "logs2006*" & "logs2007*" files. (4 Replies)
Discussion started by: thepurple
4 Replies
Login or Register to Ask a Question