couting occurences of a character inside a string and assigning it to a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users couting occurences of a character inside a string and assigning it to a variable
# 1  
Old 08-03-2011
couting occurences of a character inside a string and assigning it to a variable

echo "hello123" | tr -dc '[0-9]' | wc -c

using this command i can count the no of times a number from 0-9 occurs in the string "hello123"
but how do i save this result inside a variable?

if i do
x= echo "hello123" | tr -dc '[0-9]' | wc -c

that does not work...plz suggest..thanks
# 2  
Old 08-03-2011
Code:
x=`echo "hello123" | tr -dc '[0-9]' | wc -c`

And don't put spaces around the equal sign.
This User Gave Thanks to yazu For This Post:
# 3  
Old 08-03-2011
thanks so much...works perfectly...thanks a lot Smilie
# 4  
Old 08-03-2011
Code:
$ echo "abc123" | fold -w 1 | grep -c "[0-9]"
3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script stops running after assigning empty string for a variable

Hi, This is the first time I see something like this, and I don't why it happens. Please give me some help. I am really appreciate it. Basically I am trying to remove all empty lines of an input.. #!/bin/bash set -e set -x str1=`echo -e "\nhaha" | grep -v ^$` #str2=`echo -e "\n" |... (4 Replies)
Discussion started by: yoyomano
4 Replies

2. Shell Programming and Scripting

How to change last character in string with a variable value.

Hey Guys, I have text such as this. 28003,ALCORN,2 28009,BENTON,2 28013,CALHOUN,2 28017,CHICKASAW,2 47017,CARROLL,2 05021,CLAY,0 The last digit after the final "," is a variable value. This is the base file. I have to do further execution on this file later and I need to update the... (7 Replies)
Discussion started by: chagan02
7 Replies

3. Shell Programming and Scripting

problem in assigning substr to a variable inside awk

Hi All, I have a fixed-width datafile from which i need to extract value/string starting from some position to the specified length in each of the lines. awk '{print substr($0,x,y)}' datafile --- is working fine but awk 'BEGIN{a=0}{a=substr($0,x,y);print $a}' datafile ---is giving... (3 Replies)
Discussion started by: loggedin.ksh
3 Replies

4. Shell Programming and Scripting

Problems assigning a string to a variable

Hello everyone, My problem looks quite simple , how to assign a string with spaces and lines "\n" to a variable. I've tried all kind of quoting and is impossible, bash always try to execute the string and never executes a simple assignation. This is part of the code ... (1 Reply)
Discussion started by: trutoman
1 Replies

5. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

6. Shell Programming and Scripting

How to remove the first character on a string in a variable

Hi all, Does anyone know how to code in ksh that will remove the first character in a string variable and replace that variable without the first character? Example: var1=ktest1 will become var1=test1 var2=rtest2 will become var2=test2 Need help please. (10 Replies)
Discussion started by: ryukishin_17
10 Replies

7. Shell Programming and Scripting

delete last character in all occurences of string

Hello all, I have a file containing the following p1 q1 p2 q2 p1 p2 p3 pr1 pr2 pr1 pr2 pa1 pa2 I want to remove the last character from all strings that start with 'p' and end with '1'. In general, I do not know what is between the first part of the string and the last part of the string.... (4 Replies)
Discussion started by: bigfoot
4 Replies

8. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

9. UNIX for Dummies Questions & Answers

check if a character is inside a variable

Good morning forums. Ive been trying all weekend to do this with no luck, so i have come to the forums for help. What i want to do is check if an inputed character is inside a variable. for example, if var=1 2 3 4 5 6 7 8 9 0 i would like to see if a newly inputed number is already... (5 Replies)
Discussion started by: strasner
5 Replies

10. UNIX for Advanced & Expert Users

How to count no of occurences of a character in a string in UNIX

i have a string like echo "a|b|c" . i want to count the | symbols in this string . how to do this .plz tell the command (11 Replies)
Discussion started by: kamesh83
11 Replies
Login or Register to Ask a Question