LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2008, 04:29 AM   #1
kalimat
Member
 
Registered: Dec 2008
Posts: 30

Rep: Reputation: 0
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,,
 
Old 12-11-2008, 04:46 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 12-11-2008, 04:48 AM   #3
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Code:
NR_LINES=$(cat $FILE | wc -l)
 
Old 12-11-2008, 09:58 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jcookeman View Post
Code:
NR_LINES=$(cat $FILE | wc -l)
no need cat
Code:
wc -l < file
 
Old 12-11-2008, 10:14 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Other methods, using awk and sed:
Code:
awk 'END{print NR}' file
sed -n '$=' file
 
Old 12-11-2008, 10:59 AM   #6
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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.
 
Old 12-11-2008, 11:05 AM   #7
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
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
 
Old 12-14-2008, 02:02 AM   #8
arunmathew1984
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
Use the Word Count tool (wc) on linux.

Linux Archive

Last edited by arunmathew1984; 12-20-2008 at 11:25 AM.
 
Old 12-14-2008, 10:07 AM   #9
kalimat
Member
 
Registered: Dec 2008
Posts: 30

Original Poster
Rep: Reputation: 0
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!!!!!!
 
Old 12-14-2008, 10:09 AM   #10
kalimat
Member
 
Registered: Dec 2008
Posts: 30

Original Poster
Rep: Reputation: 0
i forgot to mention that the path of the files are well-written (not this is the problem)
 
Old 12-14-2008, 10:48 AM   #11
binary_pearl
Member
 
Registered: Jul 2007
Location: Chicago Illinois
Distribution: SLES 10 SP2/SP3, SLES 11 SP1, OpenSUSE, Sabayon, Gentoo, Fedora 14, RHEL 3/4/5/6
Posts: 98
Blog Entries: 3

Rep: Reputation: 23
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
 
Old 12-14-2008, 01:47 PM   #12
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
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
 
Old 12-14-2008, 04:58 PM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Code:
cat files | grep -c '.*'
I use this to gather statistics on my projects regularly (within a more complex script, of course.)
ta0kira
 
Old 12-14-2008, 07:58 PM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ta0kira View Post
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
 
Old 12-14-2008, 08:44 PM   #15
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by ghostdog74 View Post
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

Last edited by ta0kira; 12-14-2008 at 08:47 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to count lines from 'jobs' not working ... SlowCoder Linux - Newbie 5 05-02-2007 09:56 AM
count lines beginning with 50 chloraldo Programming 3 08-18-2006 08:30 AM
count total number of lines in several files xushi Programming 5 11-12-2005 04:42 PM
command to count how many lines in a file? dr_zayus69 Linux - Software 3 01-06-2005 10:42 AM
How to count how many lines of code my project is? The_Nerd Programming 4 08-30-2004 08:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration