Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need to count no. of commas in each line of a csv file which contains text with spaces:
e.g. below text in a file
[mig@cpeuidb bin]$ cat cmt.txt_bkp
this, is a ,comma ,count test1
this, is a ,comma ,count test2
this, is a ,comma ,count test3
Thisisaline,withoutspace,test1
Thisisaline,withoutspace,test2
this, line is ,lacking comma test1
this, line is ,lacking comma test2i had written script with for loop, problem is, whenever there is a space it splits text into new line, so incorrect count of commas in a line: Please help me out to resolve this problem:
Here is my code for references:[mig@cpeuidb bin]$ cat cmt.ksh
#!/bin/ksh. /home/mig/dm_setup
for k in `cat cmt.csv `
do
echo $k > tmp.lst
line_comma_cnt=`awk ' BEGIN { count=0; }
{ for (i=1;i<=length($0);i++)
{
if(substr($0,i,1)=="," ) {count++;}
}
}
END {print count} ' tmp.lst`
echo "New count: $line_comma_cnt"done
exit 0

![]() |
PHP image gallery
|
Build up error in python ...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |