user2860658
user2860658

Reputation: 153

Print comma separated value in BASH

How would one go about printing a comma separated value without the comma. For example

John,Smith 

would print out

John Smith

Upvotes: 0

Views: 2103

Answers (4)

glenn jackman
glenn jackman

Reputation: 246764

Assuming you're reading from a file:

while IFS=, read -ra names; do
    echo "${names[*]}"
done < file

The first line iterates over the lines of the file, reading each line into the array "names", using comma as the field separator. The IFS variable is set only for the duration of the read command. Then the array is joined into a single string, using the first char of the default value of IFS (a space) as the join characters.

Upvotes: 1

Saucier
Saucier

Reputation: 4360

I'd choose parameter expansion as my weapon. To be more specific I'd use the search and replace feature of bash parameter expansion.

$ name="John,Smith"
$ echo "${name/,/ }"
John Smith

Upvotes: 2

hlovdal
hlovdal

Reputation: 28180

You can substitute the comma with a space character. Common tools for this is either sed or tr:

NAME="John,Smith"
echo $NAME | tr , ' '
echo $NAME | sed 's/,/ /g'

Upvotes: 0

user3392484
user3392484

Reputation: 1919

Use tr to replace commas with spaces?

echo 'John,Smith' | tr ',' ' '

Upvotes: 0

Related Questions