LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-08-2010, 11:38 AM   #1
zaeem
Member
 
Registered: Jan 2010
Posts: 118

Rep: Reputation: 15
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.
 
Old 10-08-2010, 11:51 AM   #2
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
You can use `wc -l` to count the number of rows.
 
Old 10-08-2010, 11:57 AM   #3
suprstar
Member
 
Registered: Aug 2010
Location: Atlanta
Distribution: ubuntu, debian
Posts: 142
Blog Entries: 2

Rep: Reputation: 22
Try:

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

  done

echo "Total rows=$rows"
 
Old 10-08-2010, 04:44 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Quote:
Originally Posted by zaeem View Post
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.
 
Old 10-08-2010, 08:35 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
DBD::mysql::st execute failed: Column count doesn't match value count at row 1 shifter Programming 2 02-24-2010 08:42 PM
How can I use Shell script to edit row 23 column 5-8 in a txt file? leena_d Linux - Newbie 4 12-14-2009 04:43 AM
Having a problem getting my shell script to send an email after each row. mgsmith7475 Linux - Newbie 3 04-27-2009 09:28 PM
Getting Shell Script to Execute IF new row in Postgres SQL is added? (PHP) gatorpower Programming 1 03-18-2009 01:45 AM
Shell script to parse csv-like output, row by row utahnix Linux - General 8 12-08-2007 06:03 AM


All times are GMT -5. The time now is 06:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration