![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
Code:
my $f = "sample_file_name"; open (F, $f) || die "Could not open $f: $!\n"; my @f = <F>; close F; my $lines = @f; Code:
no_lof_line=`wc -l <sample_file_name` |
|
|||
|
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. |
|||
| Google UNIX.COM |