The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find number of lines in a file somesh_p Shell Programming and Scripting 2 12-19-2007 05:15 PM
total number of lines in a file Raynon Shell Programming and Scripting 9 10-04-2007 06:13 AM
cut a number of lines out of a file networkfre@k Shell Programming and Scripting 1 12-08-2005 09:46 PM
Need ls to show number of lines in each file GMMike UNIX for Dummies Questions & Answers 1 11-19-2004 01:53 AM
Counting The Number Of Duplicate Lines In a File crunchtime UNIX for Dummies Questions & Answers 2 07-04-2003 10:24 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-19-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 128
Arrow Number of lines in a file (perl script)

Hi All,

The below command gives the number of lines in a file

perl -le 'print $==()=<>' <file_name>

I want to use this command inside a perl script and store the
output in a variable. How can i do this ?

Is their any other methods to adopt this?

Thanks in advance
JS
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-20-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,707
Your code already assigns the number of lines to the variable $= (which is a really obscure thing to do anyway); just change that to whatever variable you really want.

More generally, if you read a file into an array, then @array in scalar context will tell you how many lines are in that array. Presumably you want to do something more with the file than just count the number of lines in it.
Reply With Quote
  #3 (permalink)  
Old 05-20-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 128
Thanks Era .
Actually i got the code from the internet and didn't bother to find how it works (silly of me)..
I just want to store the number of lines in a file in the variable.

I use to do the following inside a shell script (say 1.sh)
wc -l "sample_file_name" > 1.txt
no
no_lof_line=`cut -d" " -f1 1.txt`

I need to use the same funda here "inside" a perl script ( say test.pl)
I still doubt whether the code i said in the first thread still works inside a perl script.
Reply With Quote
  #4 (permalink)  
Old 05-20-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,707
Code:
my $f = "sample_file_name";
open (F, $f) || die "Could not open $f: $!\n";
my @f = <F>;
close F;
my $lines = @f;
Incidentally, in the shell script, if you use redirection, you don't need to use cut to get rid of the file name.

Code:
no_lof_line=`wc -l <sample_file_name`
And of course, the temporary file is also quite unnecessary, either way.
Reply With Quote
  #5 (permalink)  
Old 05-20-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 128
Thanks a lot Era ,

But can we compress this code ?
And, in perl, dont we have a single command to get the line count ?

JS
Reply With Quote
  #6 (permalink)  
Old 05-20-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,707
With perl -le '...' filename you get the open/close dance for free. In a more complex script, you probably don't want that. If you have the script set up to read the file in question then you can skip the open/close, and simply do my @f = <> like in your original one-liner.

If you already have the file's contents in a variable $string, then you can count the newlines in there with a simple $lines = () = $string =~ m/\n/g -- this is a cryptic shorthand for a rather complex series of commands which would again take multiple lines in longhand, unobfuscated form.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0