Counting number of commas(,) in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting number of commas(,) in a variable
# 1  
Old 06-24-2009
Counting number of commas(,) in a variable

Hi all,

I am having problems counting commas (,) from a variable in shell scripting..

the variable contains similiar to: ID@NAME@DESCRIPTION,ID@NAME@DESCRIPTION, .....
It can go on and on..

So i need to count the number of sets i.e.( ID@NAME@DESCRIPTION is one set) and process the informattion in them.

Does anyone knows how to count the number of commas in this example? or is there any other sleek method to do this =)
# 2  
Old 06-24-2009
Something like this?

Code:
echo $var | awk -F, '{print NF-1}'

# 3  
Old 06-24-2009

Code:
var='ID@NAME@DESCRIPTION,ID@NAME@DESCRIPTION, .....'
printf "%s\n" "$var" | awk '{ print gsub(/,/,"") }'

Or, in bash or ksh93:

Code:
x=${var//[!,]/}
echo "${#x}"


Last edited by cfajohnson; 06-24-2009 at 06:15 AM..
# 4  
Old 06-25-2009
hmm i get it working!~ thanks.

after i counted the comma(s),

i store the value into COUNT variable and do a while loop
and also, var='ID@NAME@DESCRIPTION,ID@NAME@DESCRIPTION, .....'

i also wanted to extract each individual person info out based on the commas between them.

But, somehow, the awk function print $i can't work.
the value stored in the $person is still the same as $var

Code:
i=0
while [ $i -le $COUNT ] ; do
        i=`expr $i + 1`
        person=`echo $var | awk 'BEGIN { FS = "," } ; { print $($i) }'`
        process each person..



---------- Post updated at 11:34 PM ---------- Previous update was at 11:06 AM ----------

anyone knows how to solve?
# 5  
Old 06-25-2009
Code:
$
$ echo "i1@n1@d1,i2@n2@d2,i3@n3@d3" |perl -nle 'chomp; split/,/; foreach $i(@_){print $i}'
i1@n1@d1
i2@n2@d2
i3@n3@d3
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help: Counting values less than a number

So I have several files (35000, to be exact) in the format rmsd_protein_*.dat each with 2 columns and 35000 rows. I would like to count how many values in the second column are less than 3 for each file, and output it into a new file so that it ultimately appears as: 1 14057 2 ... (12 Replies)
Discussion started by: Alexandryne
12 Replies

2. Shell Programming and Scripting

Checking number of commas in each line.

Hi All, I am checking whether each line is having "n" number of commas or nor. In case not then I need to exit the process. I tried cat "$TEMP_FILE" | while read LINE do processing_line=`expr $processing_line + 1` no_of_delimiters=`echo "$LINE" | awk -F ',' '{ print NF }'` if ... (4 Replies)
Discussion started by: Anupam_Halder
4 Replies

3. Shell Programming and Scripting

Counting the number of characters

Hi all, Can someone help me in getting the following o/p I/p:... (7 Replies)
Discussion started by: Sri3001
7 Replies

4. Shell Programming and Scripting

counting number of sentence

Hi all I want to count total numbers of sentences separated by fullstop (.) in different files under a directory at one go. Any help is appreciated. (3 Replies)
Discussion started by: my_Perl
3 Replies

5. Shell Programming and Scripting

need help with counting of files then pass number to variable

hi all, i'm trying to pass a count of files to a variable thru these set of codes: sh_count=$(ls -1 fnd_upload_LV*.* |wc -l) problem is if no files matches that, it will give an error "ls: fnd_upload_LV*.*: No such file or directory". how do i avoid having the shell script show that... (2 Replies)
Discussion started by: adshocker
2 Replies

6. Shell Programming and Scripting

counting the number of occurences

say i've got a text file with >10million sequences: ssss ssss tttttt uuuuuu uuuuuu uuuuuu ... I'd like to convert the file so that the output will report the number of occurence right by each sequence: 2 ssss 2 ssss 1 tttttt 3 uuuuuu 3 uuuuuu 3 uuuuuu .... (3 Replies)
Discussion started by: johjoh
3 Replies

7. Shell Programming and Scripting

counting the number of lines - again

Hi all, I use bash shell and I have a problem with wc. I would like to determine the number of lines in a file so I do wc -l filename but I don't want to get the filename again I just would like to have the number of lines and use it in a variable. Can anybody help? Thank you, (7 Replies)
Discussion started by: f_o_555
7 Replies

8. UNIX for Dummies Questions & Answers

Inserting commas and replacing backslashes with commas

Hi, Newbie here. I have a file that consists of data that I want to convert to a csv file. For example: Jul 20 2008 1111 / visit home / BlackBerry8830/4.2.2 Profile/MIDP-2.0 Configuration/CLOC-1.1 VendorID/105 Jul 21 2008 22222 / add friend / BlackBerry8830/4.2.2 Profile/MIDP-2.0... (3 Replies)
Discussion started by: kangaroo
3 Replies

9. UNIX for Dummies Questions & Answers

Counting number of occurences

Hi All, I have to count the number of occurences of the character " ; " in a given line. I had used the following awk command to achieve the same echo $KOP.dat|awk '{split($1,my,";"); for(i in my)c++ }END{print c-1}' My file KOP.dat had the following data ... (1 Reply)
Discussion started by: kingofprussia
1 Replies

10. Linux

counting the number of lines

Hello, I have afile which begins with a few urls on multiple lines and then there is listing of some information on separate lines. The listing begins with the word Name on a given line followed by teh actual list. I want to count the number of lines in this file after the line having... (6 Replies)
Discussion started by: nayeemmz
6 Replies
Login or Register to Ask a Question