Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace one character with another in Bash

I need to replace a space ( ) with a dot (.) in a string in bash.

I think this would be pretty simple, but I'm new so I can't figure out how to modify a similar example for this use.

like image 234
Brian Leishman Avatar asked May 08 '11 14:05

Brian Leishman


People also ask

How do I replace text in Bash?

To replace content in a file, you must search for the particular file string. The 'sed' command is used to replace any string in a file using a bash script. This command can be used in various ways to replace the content of a file in bash. The 'awk' command can also be used to replace the string in a file.

How do I replace a character in a string in another?

The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.

How do you replace characters with tr?

The `tr` command uses –s (–squeeze-repeats) option for search and replace any string from a text. In the following example, space (' ') is replaced by tab ('\t').


1 Answers

Use inline shell string replacement. Example:

foo="  "  # replace first blank only bar=${foo/ /.}  # replace all blanks bar=${foo// /.} 

See http://tldp.org/LDP/abs/html/string-manipulation.html for more details.

like image 97
Brian Clapper Avatar answered Sep 25 '22 12:09

Brian Clapper