Delete a comma from string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete a comma from string
# 1  
Old 03-26-2009
Delete a comma from string

Hey guys,

Its a simple question though, but since I'm new to this shell scripting world ... it's kind of difficult.

Say I have some string as :

ListenAddress=ABCServer1,ABCServer2

I want to output the value of ListenAddress as ...

ABCServer1 ABCServer2

so, basically, I want to display the string, delete the comma in between the two servers and replace it with a white space.

Can any one help ??
# 2  
Old 03-26-2009
Code:
$ echo "ListenAddress=ABCServer1,ABCServer2" | awk -F "=" '{print $2}' | sed 's/,/ /g'
ABCServer1 ABCServer2

# 3  
Old 03-26-2009
In most cases the combinations of awk/sed/grep piping into each other call for a simpler solution:
Code:
echo 'ListenAddress=ABCServer1,ABCServer2' | nawk -F'[=,]' '{print $2,$3}'

# 4  
Old 03-26-2009
Hammer & Screwdriver Here is another approach

Code:
> echo $ListenAddress                      
ABCServer1,ABCServer2
> echo $ListenAddress | tr "," " "
ABCServer1 ABCServer2

# 5  
Old 03-26-2009
Thanks a lot mates
# 6  
Old 03-26-2009
by the way, can i assign the out put of this to a variable ??
# 7  
Old 03-26-2009
Quote:
Originally Posted by MisterKhan
by the way, can i assign the out put of this to a variable ??
Sure:

Both servers into the same variable:

(you can use a different assignment variable in the read statement if needed)

Code:
oldIFS="$IFS"
IFS=","
echo $ListenAddress | read ListenAddress
IFS="$oldIFS"
echo ListenAddress
ABCServer1 ABCServer2

Or

Each server into it's own variable:

Code:
oldIFS="$IFS"
IFS=","
echo $ListenAddress | read server1 server2
IFS="$oldIFS"
echo $server1
ABCServer1
echo $server2
ABCServer2

both examples assume you only have the two servers in the "ListenAddress" variable to begin with.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to convert a comma delimited string to records or lines of text?

Hi, I am not sure if I've posted this question before. Anyway, I previously asked about converting lines of text into a comma delimited string. Now I am needing to do the other way around ... :( :o Can anyone advise how is this possible? Example as below: Converting records/lines to... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. UNIX for Dummies Questions & Answers

Handling Comma in string values in a CSV file

Hi have a comma separated file which has numeric and string columns. String columns are quoted and can have comma in between the quotes. How to identify the columns with FS ="," sample records"prabhat,kumar",19,2000,"bangalore,India" In awk it should be$1 = prabhat,kumar $2=19 $3=2000... (9 Replies)
Discussion started by: prabhat.diwaker
9 Replies

3. UNIX for Dummies Questions & Answers

How to change a line of text to a comma delimited string?

Hi, Is there a one-liner that I can use to change a line of text into a comma delimited string? For example, convert user1 user2 user3 user4to user1,user2,user3,user4Currently using while read x, although got the extra comma at the end that I have to remove manually. Please... (5 Replies)
Discussion started by: newbie_01
5 Replies

4. Shell Programming and Scripting

Egrep how to make sure string is after 5 comma

Hello, Need help... using egrep how do I make sure string is after 5th comma example: a,b,c,d,e,f,g,h,i Suppose i want to search letter f but want to make sure it is after 5th comma. Is there any way to check string is after 5th comma? Thanks !! (9 Replies)
Discussion started by: vegasluxor
9 Replies

5. Shell Programming and Scripting

Inserting string in between field in comma separated file

Hello Mates, I have one txt file having commo seperated values. I have to insert string "FALSE" in 2nd field from the end. E.G SE18 6RN,,,,5439070,1786840,,1000002148671600,123434 Out put should be: SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434 Can some one help me to... (8 Replies)
Discussion started by: krsnadasa
8 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

Replace a value after 13th comma in a string

Suppose b=50,0,0,40,1,0,5000,gold,0,0,0,0,32,9,2,0,10000,0,0,0,0,0,0,0,0,0,BSNL_SMS_Bundle ,null,null,null,null,null,null,null,null,null,0,0,0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,0,0,null,null,0,0,405564245,0 c=11 After 13th comma, the value of 9 needs to be changed and to be filled by another... (4 Replies)
Discussion started by: karan23kohli
4 Replies

8. Shell Programming and Scripting

Awk: Remove comma at the end of the string

Hi i had String like UID: ABC345QWE678GFK345SA90, LENGTH 32 when I used awk ' FS, {print $1}' prints ABC345QWE678GFK345SA90, how can i getrid of that coma at the end of the string. Thanks in advance.. (14 Replies)
Discussion started by: Reddy482
14 Replies

9. Shell Programming and Scripting

comma separated string manipulation

hi, i have a script where i am accepting a comma separated string from the user, i have to separated those strings on the basis of comma and store it in variables.. below is the script #!/bin/ksh clear echo "Enter the strings seperated by commas :- \c " read strn echo $strn... (2 Replies)
Discussion started by: saharookiedba
2 Replies

10. Shell Programming and Scripting

How do you delete multiple text from a comma delimited file

I would like to know code that will delete multiple text from a comma delimited file. For example, how would the comma delimited file below delete the word 'PEST' in Perl language (previously an excel file that was converted to a csv and the last column was PEST): 1, 2,43,34, bosx,PEST 1,... (1 Reply)
Discussion started by: dolo21taf
1 Replies
Login or Register to Ask a Question