how to find the count of commas in a string excluding the ones in double quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find the count of commas in a string excluding the ones in double quotes
# 1  
Old 06-12-2010
how to find the count of commas in a string excluding the ones in double quotes

Hi,
my requirement is to find the count of commas in a string excluding the ones in double quotes.
For example:
If the input string is

abc,xyz.com,lmhgdf,"abc, 401 street","tty,stt",45,23,45

The output should be 7
# 2  
Old 06-12-2010
Code:
awk '{gsub("\"[^\"]*\"","");n=gsub(",","");print n}' infile

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-12-2010
Code:
$ echo 'abc,xyz.com,lmhgdf,"abc, 401 street","tty,stt",45,23,45' | \
sed 's/"[^"]*"//g' | grep -o , | wc -l
       7
$

This User Gave Thanks to pseudocoder For This Post:
# 4  
Old 06-14-2010
perl regexp may be a good helper

Code:
my $str='abc,xyz.com,lmhgdf,"abc, 401 street","tty,stt",45,23,45,aa,bb,"a,b","a,bb,aaa,ss,,,,,",a,a,a,a';
my @tmp = $str=~/(,)(?=
[^"]*$
|
(?:(?:[^",]*,)*(?:"[^"]*",)*(?:[^",]*,)*)*(?:[^",]+|"[^"]*")$
)/xg;
print $#tmp+1;

# 5  
Old 06-28-2010
:)

Thanks a lot guys...
This worked for me !!!
# 6  
Old 06-28-2010
And another one:

Code:
perl -MText::ParseWords -nle'
    print parse_line(",",0, $_) - 1;
    ' infile


Last edited by radoulov; 06-28-2010 at 05:14 PM..
# 7  
Old 06-28-2010
Using bartus11
Code:
$ echo 'abc,xyz.com,lmhgdf,"abc, 401 street","tty,stt",45,23,45' | awk -F"," ' { gsub("\"[^\"]*\"",""); print NF-1 } '
7

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script that should remove unnecessary commas between double quotes in CSV file

i have data as below 123,"paul phiri",paul@yahoo.com,"po.box 23, BT","Eco Bank,Blantyre,Malawi" i need an output to be 123,"paul phiri",paul@yahoo.com,"po.box 23 BT","Eco Bank Blantyre Malawi" (5 Replies)
Discussion started by: mathias23
5 Replies

2. Shell Programming and Scripting

How to match fields surrounded by double quotes with commas?

Hello to all, I'm trying to match only fields surrounded by double quotes that have one or more commas inside. The text is like this "one, t2o",334,"tst,982-0",881,"kmk 9-l","kkd, 115-001, jj-3",5 The matches should be "one, t2o" "tst,982-0" "kkd, 115-001, jj-3" I'm trying with... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

3. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

4. Shell Programming and Scripting

How to delete the commas in a .CSV file that are enclosed in a string with double quotes?

Okay, I would like to delete all the commas in a .CSV file (TEST.CSV) or at least substitute them with empty space, that are enclosed in double quote. Please see the sample file as below: column 1,column 2,column 3,column 4,column 5,column 6,column 7,column 8,column 9,column 10... (8 Replies)
Discussion started by: dhruuv369
8 Replies

5. Shell Programming and Scripting

How to find a string with double quotes?

I have thousands of files in a directory. I need to find/list all files that have the below matching string - RETURNCODE: "1017" Thank you! (5 Replies)
Discussion started by: esmgr
5 Replies

6. Shell Programming and Scripting

Preserve commas inside double quotes (perl)

Hi, I have an input file like this $ cat infile hi,i,"am , sam", y hello ,good, morning abcd, " ef, gh " ,ij no, "good,morning", yes, "good , afternoon" from this file I have to split the fields on basis of comma"," however, I the data present inside double qoutes should be treated as... (3 Replies)
Discussion started by: sam05121988
3 Replies

7. Shell Programming and Scripting

HELP with AWK or SED. Need to replace the commas between double quotes in CSV file

Hello experts, I need to validate a csv file which contains data like this: Sample.csv "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 I just need to check if all the records contain exactly the number of... (5 Replies)
Discussion started by: shell_boy23
5 Replies

8. Shell Programming and Scripting

Get string between quotes separate by commas

I'm a beginner with shell and tried to do this per hours and everytinhg gives different want i do. So I have a lot of file in *.csv ( a.csv, b.csv ...) in each file csv , it has some fields separeted by commas. ----- "joseph";"21","m";"groups";"j.j@gmail.com,j.j2@hotmail.com"... (6 Replies)
Discussion started by: flaviof
6 Replies

9. Shell Programming and Scripting

How to replace spaces excluding those within double quotes and after backslash?

In bash or perl, I would like to know how to substitute a null character (0x00) for every white space without changing the white spaces inside the block of double quotes and the white space immediately following a backslash. Suppose that sample.txt consists of the following line. "b 1" c\ 2 ... (2 Replies)
Discussion started by: LessNux
2 Replies

10. Shell Programming and Scripting

How to count number of double quotes in bash

Need a little help. I have just a simple string with a lot double quotes in it. I need to be able to parse through this string, and know how many double quotes I have, and where I am, so I can key off every 9th double quote. For example (coding is not complete): #!/bin/bash count=0... (3 Replies)
Discussion started by: Akilleez
3 Replies
Login or Register to Ask a Question