intoto
intoto

Reputation: 43

Mac OS X Unix to find replace using regular expression

this is my first question so apologies if this has been answered before. I have searched but cannot find what I am looking for.

I have a huge amount of text that has been exported from a database. For certain attribute content, I need to find each instance of a space followed by a capital letter and just replace the space with a semi-colon(;).

For example:

First one Second one Third one Fourth one

Should become:

First one;Second one;Third one;Fourth one

Using Unix (Mac OS X) The following just returns the whole line:

echo "First one Second one Third one Fourth one" | grep ' [A-Z]'

How would I achieve my desired outcome please? Any help or pointers would be very much appreciated. Thanks.

Upvotes: 4

Views: 3314

Answers (1)

falsetru
falsetru

Reputation: 369074

Use sed for such task:

$ echo "First one Second one Third one Fourth one" | sed -Ee 's/ ([A-Z])/;\1/g'
First one;Second one;Third one;Fourth one
  • -E option is specified to use extended regular expression.

  • s/pattern/replace-string/g finds pattern and replace with the replace-string globally.
  • [A-Z] matches uppercase alphabet. by surrounding the pattern with (..), it can be referenced later in replace-string.

Upvotes: 2

Related Questions