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 and shell scripting languages 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 09:15 PM
total number of lines in a file Raynon Shell Programming and Scripting 9 10-04-2007 10:13 AM
cut a number of lines out of a file networkfre@k Shell Programming and Scripting 1 12-09-2005 01:46 AM
Need ls to show number of lines in each file GMMike UNIX for Dummies Questions & Answers 1 11-19-2004 05:53 AM
Counting The Number Of Duplicate Lines In a File crunchtime UNIX for Dummies Questions & Answers 2 07-04-2003 02:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-20-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
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
  #2 (permalink)  
Old 05-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
  #3 (permalink)  
Old 05-20-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
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.
  #4 (permalink)  
Old 05-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652

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.
  #5 (permalink)  
Old 05-20-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
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
  #6 (permalink)  
Old 05-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0