![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | 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 |
|
||||
|
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; 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. |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|