Testing for multiple words in a Variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Testing for multiple words in a Variable
# 1  
Old 02-17-2009
Testing for multiple words in a Variable

Hi,

Been mulling this one over today and can't seem to find the exact method to solve this. So I assign a value to a variable based on a grep. Usually that variable has 1 value but today I realized that depending on the text I parse, it might get 2 or 3 or more. The result comes out like the following:

value1 value2 value3

So what I want to do is create a loop to handle these cases by testing if the variable has multiple words like the above and if it does, I want to substitute a comma in place of the spaces so the result would be:

value1,value2,value3

Any help is appreciated, thanks!
# 2  
Old 02-17-2009
Quote:
Originally Posted by eltinator
Hi,

Been mulling this one over today and can't seem to find the exact method to solve this. So I assign a value to a variable based on a grep. Usually that variable has 1 value but today I realized that depending on the text I parse, it might get 2 or 3 or more. The result comes out like the following:

value1 value2 value3

So what I want to do is create a loop to handle these cases by testing if the variable has multiple words like the above and if it does, I want to substitute a comma in place of the spaces so the result would be:

value1,value2,value3

Any help is appreciated, thanks!
Here is one way to do it.

out="value1 value2 value3"
Code:
out=$(echo ${out} | tr " " ",")

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. Shell Programming and Scripting

How to replace multiple words together?

I am looking for any smart perl solution for multiple word replacement together. My awk is working but for big files it is superslow. awk 'NR==FNR {a=$2;next} {for ( i in a) gsub(i,a)}1' new_word_list.txt oldfile > newfile new_word_list.txt looks like AB733 ST756 AB734 ST219 AB11 ... (4 Replies)
Discussion started by: sammy777888
4 Replies

3. Shell Programming and Scripting

Find Multiple words in a file

Hi all, I have trouble in finding the multiple word in .txt file. Please help me with any solution. I have 10,000 .txt files and in each file i have to search specific word but more than one, like (data, machine learning, clustering) and all these keywords should be case insensitive because... (3 Replies)
Discussion started by: XXLMMN
3 Replies

4. Shell Programming and Scripting

Confused with grep for multiple words

Hi guys and gals, I have many files that contains many lines of data. I am trying to find a needle in a haystack in that I'm looking only for files that contain word1 AND word2. I'm using ... ... but this is finding files that contains word1 OR word2. No good for me. How can I grep to... (7 Replies)
Discussion started by: bbbngowc
7 Replies

5. Emergency UNIX and Linux Support

using multiple iperf instances for performance testing

Hello all! I have the need to performance-test a MPLS switch, I was thinking of using iperf to accomplish the task. I had in mind using a linux box with a Gigabit interface connected to a L2 switch on a 802.1Q trunk. In the interface I would create 20 VLANs with 20 different IP subnets. ... (2 Replies)
Discussion started by: ppucci
2 Replies

6. IP Networking

using multiple iperf instances for performance testing

Hello all! I have the need to performance-test a MPLS switch, I was thinking of using iperf to accomplish the task. I had in mind using a linux box with a Gigabit interface connected to a L2 switch on a 802.1Q trunk. In the interface I would create 20 VLANs with 20 different IP subnets. ... (0 Replies)
Discussion started by: ppucci
0 Replies

7. UNIX for Dummies Questions & Answers

search multiple words using grep

Hi frnds i want to desplay file names that should be word1 and word2 ex : i have 10 *.log files 5 files having word1 and word2 5 files having only word1, i have used below command egrep -l 'word1|word2' *.log its giving all 10 files, but i want to display only 5... (20 Replies)
Discussion started by: pb18798
20 Replies

8. Shell Programming and Scripting

need help testing for length of variable

Hello, I'm new to shell scripting and need a little help please. I'm working on a script that asks the user to input a name that can be 1 to 12 alphanumeric characters and can have dots(.) dashes(-) and spaces. I want to test that the answer is valid and if not make the user try again. I have no... (4 Replies)
Discussion started by: wlewis
4 Replies

9. Shell Programming and Scripting

greping out multiple words

now i have a lot of words i need to grep out of a certain line but i have to keep typing this: ex ls -l | grep -v this | grep -v that | grep -v those | grep -v them ..... i mean, it goes on like that. there has to be a way to grep all those words without having to specify that "grep... (5 Replies)
Discussion started by: Terrible
5 Replies

10. AIX

Variable Testing for size

How do you test a variable size to know if it is empty or not? Thanks (4 Replies)
Discussion started by: jango
4 Replies
Login or Register to Ask a Question