LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Row count Shell Script (https://www.linuxquestions.org/questions/linux-newbie-8/row-count-shell-script-836969/)

zaeem 10-08-2010 10:38 AM

Row count Shell Script
 
Dear All,

I need to write shell script which can take number of files and count total rows from all CSVs and display total number of rows counted in all files. Is there any possibility of doing that using shell script and if yes then how.

Any help will greatly be appreciated.

quanta 10-08-2010 10:51 AM

You can use `wc -l` to count the number of rows.

suprstar 10-08-2010 10:57 AM

Try:

Code:

#!/bin/bash
rows=0;
for f in `ls *.CSV`;
  do
    let rows+=`wc $f | awk '{print $1}'`

  done

echo "Total rows=$rows"


crts 10-08-2010 03:44 PM

Quote:

Originally Posted by zaeem (Post 4121559)
Dear All,

I need to write shell script which can take number of files and count total rows from all CSVs and display total number of rows counted in all files. Is there any possibility of doing that using shell script and if yes then how.

Any help will greatly be appreciated.

You have several options to achieve that, e.g.
Code:

cat *.csv | wc -l
# or
wc -l *.csv

and there are probably many more options.

ghostdog74 10-08-2010 07:35 PM

Quote:

Originally Posted by suprstar (Post 4121572)
Try:

Code:

#!/bin/bash
rows=0;
for f in `ls *.CSV`;
  do
    let rows+=`wc $f | awk '{print $1}'`

  done

echo "Total rows=$rows"



don't do this
Code:

for f in `ls *.CSV`;
do this
Code:

for f in *.CSV
and you can get count this way without calling extra awk
Code:

wc -l < file


All times are GMT -5. The time now is 02:17 AM.