ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would like to ask your opinion:
I made a script to count the lines of a file like that:
#!/bin/bash
NR_LINES=$(nl $FILE | tail -1)
echo "The file $FILE has $NR_LINES lines" >no_lines.txt
the problem is , when i execute ./count_lines.sh /home/.../file.txt it shows me the nr of lines without counting the empty lines ...
What i can i add to the script to be fine? i tried all the solutions on google but no result
Ghostdog++ - no need for cat. Using awk or sed will also avoid the leading blank spaces that wc -l leaves you with.
Code:
#!/bin/bash
file="array"
awk=$(awk 'END{print NR}' $file)
wc=$(wc -l $file)
sedd=$(sed -n '$=' $file)
echo "Awk: There are $awk lines in $file."
echo "Wc -l: There are $wc lines in $file."
echo "Sed: There are $sedd lines in $file."
Output:
Code:
Awk: There are 18 lines in array.
Wc -l: There are 18 array lines in array.
Sed: There are 18 lines in array.
Last edited by Telemachos; 12-11-2008 at 11:03 AM.
#i=1
#while test $i -le $#; do
until [ -z "$1" ]; do
NR_LINES=$(cat $1 | wc -l)
echo "File $1 has $NR_LINES lines"
shift
#((i++))
1. Ultimately you were getting the "cat: 1: No such file or directory" messages because
you were trying to cat '$i', which was always going to 1, 2, 3...etc and not the actual file names passed in.
So instead, we will always use $1 to refer to the current command line argument. At the end of the loop we use the shift command, which will move the next command line argument into $1. So something like this:
1st time through loop:
$1 = file1
$2 = file2
$3 = file3
2nd time through loop:
$1 = file2
$2 = file3
3rd time through loop:
$1 = file3
2. Instead of this using the $i variable within a while statement, you can use the:
until [ -z "$1" ]; do
to loop through the arguments. That way you don't have to keep track of them through an extra variable.
That won't work because it will give each individually along with the file name (unless I've missed that that's the objective.) Is OP looking for a total among all files, or a total per file?
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.