The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to get a file name & record count of csv file sirik UNIX for Dummies Questions & Answers 2 03-06-2008 02:55 PM
How count number of fields in a record sureshg_sampat Shell Programming and Scripting 5 01-07-2008 06:30 AM
record count dr46014 Shell Programming and Scripting 4 12-11-2007 04:39 PM
Need help with Isql record count johnu122 Shell Programming and Scripting 4 11-18-2006 12:31 PM
How to count the record count in an EBCDIC file. oracle8 UNIX for Dummies Questions & Answers 1 07-26-2006 07:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2008
ajaykk ajaykk is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
Exclamation Record count of a csv file

Hello Gurus,

We have a requirement to count the valid number of records in a comma delimited file with double quotes.
The catch here is..few records have a new line carriage within the double quotes,,say for ex:we have a file called accounts the record count is 4827..but the actual valid count is 4823..1 header row and 1 field in a record has 3 lines of data.
So i'm having a tough time to count the exact count..we need to compare this count to the table count where we are loading this file to.
Please suggest..

Thanks
AJ
  #2 (permalink)  
Old 07-17-2008
ajaykk ajaykk is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
~bump~
  #3 (permalink)  
Old 07-17-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Some random thoughts

1) cat file | grep "^[0-9][0-9]" | wc -l
Or similar, if the normal file has numbers in the first couple character positions as user id or other identifier

2) cat file | cut -c1 | grep '"'
Assuming that the first field begins with a "

3) cat sample | tr "\n" " " | tr '"' "\n" | wc -l
Will produce a number. If normally three fields with " for each valid record, then take the resulting number from the command and divide by (twice the normal # fields).
The command above showed me 12 for my sample file, and I knew my sample has three fields with ". So 12/(3*2) = 2 records


p.s.
Be cautious of the "bump" to move a question. I always scan questions for those with zero responses - to work on first. Then, time permitting, go back to others that I previously skipped over. Therefore, your "bump" can have the reverse effect.
  #4 (permalink)  
Old 07-17-2008
ajaykk ajaykk is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
Quote:
Originally Posted by joeyg View Post
1) cat file | grep "^[0-9][0-9]" | wc -l
Or similar, if the normal file has numbers in the first couple character positions as user id or other identifier

2) cat file | cut -c1 | grep '"'
Assuming that the first field begins with a "

3) cat sample | tr "\n" " " | tr '"' "\n" | wc -l
Will produce a number. If normally three fields with " for each valid record, then take the resulting number from the command and divide by (twice the normal # fields).
The command above showed me 12 for my sample file, and I knew my sample has three fields with ". So 12/(3*2) = 2 records


p.s.
Be cautious of the "bump" to move a question. I always scan questions for those with zero responses - to work on first. Then, time permitting, go back to others that I previously skipped over. Therefore, your "bump" can have the reverse effect.

Thanks Joey..
cat sample | tr "\n" " " | tr '"' "\n" | wc -l..seems to be working..
when I tried..found an issue..for one of the record the data has multiple double quotes ..("xyz","abc","toys ""R"" us", "def", "hij","....)..
coz of this I'm not getting a round value count..how do I handle this?
  #5 (permalink)  
Old 07-17-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Can you count comma's?

Quotes inside quoted field - yech.
Can you count comma's then, using similar logic to my 3rd option? Or, are there comma's inside the quotes also? That is quite possible.

Perhaps try the first or second options?
The 2nd will show how many lines begin with a " character - in theory, your continued lines would not begin with a quote character.

Beyond the counting... good luck trying to decipher the file!
  #6 (permalink)  
Old 07-17-2008
ajaykk ajaykk is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
Quote:
Originally Posted by joeyg View Post
Quotes inside quoted field - yech.
Can you count comma's then, using similar logic to my 3rd option? Or, are there comma's inside the quotes also? That is quite possible.

Perhaps try the first or second options?
The 2nd will show how many lines begin with a " character - in theory, your continued lines would not begin with a quote character.

Beyond the counting... good luck trying to decipher the file!
Second option worked..Thanks alot Joey!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0