SPLIT STRING in bash shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SPLIT STRING in bash shell script
# 1  
Old 03-15-2010
SPLIT STRING in bash shell script

i need one help....

if i have a string like aaaaa,bbbbb,ccccc,aaaaa

How to to split the string and check howmany times aaaaa will be in that string?

Thanks
# 2  
Old 03-15-2010
YMMV:
Code:
echo 'aaaaa,bbbbb,ccccc,aaaaa' | nawk '{print gsub("aaaaa", "")}'
OR
echo 'aaaaa,bbbbb,ccccc,aaaaa' | nawk -F'aaaaa' '{print NF-1}'


Last edited by vgersh99; 03-15-2010 at 12:10 PM..
# 3  
Old 03-15-2010
Tools

Code:
echo 'aaa,bbb,ccc,aaa' | tr "," "\n" | grep 'aaa' -c

or (if that grep otion is not available)
Code:
echo 'aaa,bbb,ccc,aaa' | tr "," "\n" | grep 'aaa' | wc -l

# 4  
Old 03-15-2010
thanks a lot...
# 5  
Old 03-16-2010
Based on return values...
Code:
echo "aaaaa,bbbbb,ccccc,aaaaa" | perl -ne 'print s/aaaaa//g'

Code:
echo "aaaaa,bbbbb,aaaaa,ccccc,aaaaa" | awk '{ print gsub(/aaaaa/,""); }'

# 6  
Old 03-16-2010
Other way
Code:
echo "aaaaa,bbbbb,ccccc,aaaaa" | tr ',' '\n' | sort | uniq -c | sed '1!d'

# 7  
Old 03-16-2010
Code:
 
echo "aaaaa,bbbbb,aaaaa,ccccc,aaaaa" | sed 's/,/\n/g' | grep -c "aaaaa"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

2. Shell Programming and Scripting

split string into array in shell

Hi all, I want to split a string into array based on given delimiter, for example: String: "foo|bar|baz" with delimiter "|" into array: strArr to strArr with values foo, bar and baz. Thanks a lot. Roy987 (5 Replies)
Discussion started by: Roy987
5 Replies

3. Shell Programming and Scripting

Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)

Hi, I have the following requirement. There will be following text/line in a file (eg: search-build.txt) PRODUCT_VERSION="V:01.002.007.Build1234" I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get... (4 Replies)
Discussion started by: drwatson_droid
4 Replies

4. UNIX for Advanced & Expert Users

string manipulation in bash shell

Hi All, I am using a bash shell and want to the following thing. A process sends the following string to my script BACKUP_FAIL_REASON="Failed - Application Dump CDMACA-0:grep: /opt/nortel/ca/data/1245184/sd00/image1/S110907070708HIS... (4 Replies)
Discussion started by: Pkumar Sachin
4 Replies

5. Shell Programming and Scripting

On the command line using bash, how do you split a string by column?

Input: MD5(secret.txt)= fe66cbf9d929934b09cc7e8be890522e MD5(secret2.txt)= asd123qwlkjgre5ug8je7hlt488dkr0p I want the results to look like these, respectively: MD5(secret.txt)= fe66cbf9 d929934b 09cc7e8b e890522e MD5(secret2.txt)= asd123qw lkjgre5u g8je7hlt 488dkr0p Basically, keeping... (11 Replies)
Discussion started by: teiji
11 Replies

6. Shell Programming and Scripting

Bash:How to split one string variable in two variables?

Hello, I have a paramter $param consisting just of two literals and want to split it into two parameters, so I can combine it to a new parameter <char1><string><char2>, but the following code didn't work: tmp_PARAM_1=cut -c1 $PARAM tmp_PARAM_2=cut -c2 $PARAM... (2 Replies)
Discussion started by: ABE2202
2 Replies

7. Shell Programming and Scripting

bash shell script string comparison

I want to remove a line that has empty string at second field when I use cut with delimeter , like below $cat demo hello, mum hello, #!/bin/sh while read line do if then # remove the current line command goes here fi done < "demo" i got an error message for above... (4 Replies)
Discussion started by: bonosungho
4 Replies

8. Shell Programming and Scripting

Help with Split shell script

Hi all, Following is the shell script which I am using for splitting the contents from a flat file to the multiple XMLs. The problem is that this script is working file when the input file is having 10000 lines. When the number of lines increases; the performance degrades drastically. Please... (1 Reply)
Discussion started by: amtrip
1 Replies

9. Shell Programming and Scripting

Shell script to parse/split input string and display the tokens

Hi, How do I parse/split lines (strings) read from a file and display the individual tokens in a shell script? Given that the length of individual lines is not constant and number of tokens in each line is also not constant. The input file could be as below: ... (3 Replies)
Discussion started by: yajaykumar
3 Replies

10. Shell Programming and Scripting

split files by specifying a string (bash shell)

Hi all, I have a file of around 300 lines in which string "SERVER" occurs around 32 times. for eg. I need to split files like, for eg I am using this code awk '/SERVER/{n++}{print > f n}' f=/vikas/list /vikas/final But the problem is that it makes maximum of 10 files, but I... (12 Replies)
Discussion started by: vikas027
12 Replies
Login or Register to Ask a Question