Count number of column in a comma delimited file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count number of column in a comma delimited file
# 1  
Old 01-19-2012
Count number of column in a comma delimited file

I have a comma (,) delimited file.
Code:
106232145,"medicare","medicare,medicaid",789

I would like to count the number of fields in each line.
I tried the below code
Code:
awk -F ',' '{print NF-1}'

This returns me the result as 5 instead of 4. This is because the awk takes "medicare,medicaid" as two different fields instead of one field.Smilie

**In my file, the text field qualifier is "(double quotes)

Please help me with this.
This User Gave Thanks to machomaddy For This Post:
# 2  
Old 01-19-2012
Try:
Code:
perl -nle 's/".*?"//g;print s/,//g+1' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 01-19-2012
Another two:

Code:
# echo '106232145,"medicare","medicare,medicaid",789'|sed -e 's/"[^"]\{1,\}"//g'|awk '{print NF}' FS=','          
4

Code:
# echo '106232145,"medicare","medicare,medicaid",789,'|perl -lane '$count++ while ( /,[\d|"|\n]/g);print $count+1'
4

This User Gave Thanks to Klashxx For This Post:
# 4  
Old 01-19-2012
Code:
awk -F, '{gsub(/"[^"]*"/,x);print NF}'

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-19-2012
Thanks guys!! it works... Smilie
@Scrutinizer: could you please explain your code. bit curious about that
# 6  
Old 01-19-2012
Sure:
gsub(/"[^"]*"/,x)Delete every occurrence of "" and any characters in between ([^"]* means any number of characters that are not a double quote). x is uninitialized, so it contains nothing.
 The replacement is performed on $0, so then automatically the number of fields (NF) are recalculated.
print NFprint the number of fields..
S.
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 01-19-2012
Thanks Scrutinizer Smilie Smilie

---------- Post updated at 09:09 PM ---------- Previous update was at 08:16 PM ----------

Smilie
I have to insert a delimiter at the end of each record if it is not present

Code:
present data:
10620,"claim processed,paid",reported
 
expected:
10620,"claim processed,paid",reported,



can someone please help with this??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. Shell Programming and Scripting

Insert a new column with sequence number (Delimiter as comma)

Hi All, I have a file which has data like a,b c,d e,f g,h And I need to insert a new column at the begining with sequence no( 1 to n) 1,a,b 2,c,d 3,e,f 4,g,h Please let me know how to acheive this in unix (3 Replies)
Discussion started by: weknowd
3 Replies

3. Shell Programming and Scripting

Print records which do not have expected number of fields in a comma delimited file

Hi, I have a comma (,) delimited file, in which few fields are enclosed with in double quotes " ". I have to print the records in the file which donot have expected number of field with the line number. File1 ==== name,desgnation,doj,project #header#... (7 Replies)
Discussion started by: machomaddy
7 Replies

4. Shell Programming and Scripting

How to insert a sequence number column inside a pipe delimited csv file using shell scripting?

Hi All, I need a shell script which could insert a sequence number column inside a dat file(pipe delimited). I have the dat file similar to the one as shown below.. |A|B|C||D|E |F|G|H||I|J |K|L|M||N|O |P|Q|R||S|T As shown above, the column 4 is currently blank and i need to insert sequence... (5 Replies)
Discussion started by: nithins007
5 Replies

5. UNIX for Dummies Questions & Answers

How do you delete cells from a space delimited text file given row and column number?

How do you delete cells from a space delimited text file given row and column number? Letś say the row number is r and the column number is c. Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

6. Shell Programming and Scripting

Find and replace a column that has '' to NULL in a comma delimited using awk or sed

Hi this is my first time posting ever. I'm relatively new in using AWK/SED, I've been trying many a solution. I'm trying to replace the 59th column in a file where if I encounter '' then I would like to replace it with the word NULL. example 0 , '' , '' , 0 , 195.538462 change it to 0... (5 Replies)
Discussion started by: gumal901
5 Replies

7. UNIX for Dummies Questions & Answers

Comma delimited file

Hi All, I have output of sql saved in comma separated file. Now i need to read line by line this file and assign word to a unix variable for further processing Eg: Test file world, 1, 3, 4 earth,2,3,4,5 moon,1,2,3,4 Output should be word1= world word2=1 echo " first word... (7 Replies)
Discussion started by: gwrm
7 Replies

8. Shell Programming and Scripting

Insert comma based on max number of column

Hi, I am new to unix shell shell scripting. I have a specific requirement where I need to append comma's based on the max number of column in the file. Eg: If my source file look something like this, sengwa,china tom,america,northamerica smith,america walter My output file... (8 Replies)
Discussion started by: nicholas_ejn
8 Replies

9. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies

10. Shell Programming and Scripting

Comma Delimited file

I have a comma delimited file that sometimes has addresses details in. The problem is that the address detail can be seen as: "Sample House, Sample Road". When I run a script specifying the file is comma delimited I would like it to ignore comma's that are in between speech marks. Is this... (2 Replies)
Discussion started by: dbrundrett
2 Replies
Login or Register to Ask a Question