LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   script to count lines (https://www.linuxquestions.org/questions/programming-9/script-to-count-lines-689782/)

kalimat 12-11-2008 04:29 AM

script to count lines
 
Hello!

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

Have a nice day,,

colucix 12-11-2008 04:46 AM

By default nl numbers non empty lines only. To number empty lines you have to use the -ba option.
Code:

nl -ba $FILE
A more comfortable way to count lines in a file is wc -l. See man nl and man wc for further reference.

jcookeman 12-11-2008 04:48 AM

Code:

NR_LINES=$(cat $FILE | wc -l)

ghostdog74 12-11-2008 09:58 AM

Quote:

Originally Posted by jcookeman (Post 3371496)
Code:

NR_LINES=$(cat $FILE | wc -l)

no need cat
Code:

wc -l < file

colucix 12-11-2008 10:14 AM

Other methods, using awk and sed:
Code:

awk 'END{print NR}' file
sed -n '$=' file


Telemachos 12-11-2008 10:59 AM

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.


jcookeman 12-11-2008 11:05 AM

Fine. I'll just do it in pure Bash :)
 
Code:

#!/usr/bin/env bash

IFS=$'\x0A'$'\x0D'
declare -i LINE_COUNT=0

while read
do
    (( LINE_COUNT++ ))
done < $1

echo $LINE_COUNT


arunmathew1984 12-14-2008 02:02 AM

Use the Word Count tool (wc) on linux.

Linux Archive

kalimat 12-14-2008 10:07 AM

Hi!!!
Thanks for your answers...it went..
but,actually, i had to show the nr of lines for more files, and i wrote this:

#!/bin/bash

i=1
while test $i -le $#; do
NR_LINES=$(cat $i | wc -l)
echo "File $i has $NR_LINES lines" >>out.txt
((i++))

done

When i write " ./count_lines.sh file1 file2 " it always says:
cat: 1: No such file or directory
cat: 2:...
...

What is the problem?
Thanks for your answers!!!!!!

kalimat 12-14-2008 10:09 AM

i forgot to mention that the path of the files are well-written (not this is the problem)

binary_pearl 12-14-2008 10:48 AM

I would try something like this:

#!/bin/bash

#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.

Information from note 2 taken from: http://tldp.org/LDP/abs/html/othertypesv.html

--Shaun

jcookeman 12-14-2008 01:47 PM

I still don't understand why you need wc. I would be willing to wager the following is faster. Not that it most likely matters:

Code:

#!/usr/bin/env bash

IFS=$'\x0A'$'\x0D'
for file in $*
do
    local LINE_COUNT=0
    while read
    do
        let ++LINE_COUNT
    done < $file
    echo "$file: $LINE_COUNT"
done


ta0kira 12-14-2008 04:58 PM

Code:

cat files | grep -c '.*'
I use this to gather statistics on my projects regularly (within a more complex script, of course.)
ta0kira

ghostdog74 12-14-2008 07:58 PM

Quote:

Originally Posted by ta0kira (Post 3375494)
Code:

cat files | grep -c '.*'
I use this to gather statistics on my projects regularly (within a more complex script, of course.)
ta0kira

Code:

grep -c ".*" files

ta0kira 12-14-2008 08:44 PM

Quote:

Originally Posted by ghostdog74 (Post 3375618)
Code:

grep -c ".*" files

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


All times are GMT -5. The time now is 12:14 AM.