Seppo Enarvi
Seppo Enarvi

Reputation: 3663

Regex to remove occurrences of a word from text, but leave only one space between each word

Here's a problem I commonly encounter. I have a text file that contains words, one space between each word, and no spaces in the beginning or end of a line. I'd like to use e.g. sed to remove occurrences of a word, leaving only one space between each word, no spaces in the beginning or end of a line. Is this possible with one regular expression?

The best solution I can think of, requires three regular expressions.

% cat text
A B C A B C A

% sed -r 's/ ?\bA\b ?/ /g' text
 B C B C 

% sed -r 's/ ?\bA\b ?/ /g' text | sed 's/^ *//' | sed 's/ *$//'
B C B C

Upvotes: 0

Views: 60

Answers (2)

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

With sed:

AMD$ sed -r 's/\bA | A$//g' File
B C B C

Here, we are removing A with a space. We cover the 2 possibilities (a. A at beginning or anywhere in between, b. A at end).

Upvotes: 2

Jotne
Jotne

Reputation: 41456

You can use this awk

awk '{gsub(/\<A\>/,"");$1=$1}1' file
B C B C

The $1=$1 is used to clean up the output so it only has one space between word after the As are removed.


If you do like double/triple spacing to be intact in lines that does not have A, use this:

awk 'gsub(/\<A\>/,""){$1=$1}1' file
B C B C

It will only modify line with A in it

Edit: Updated with word boundary, so it does not remove part of words.

Upvotes: 2

Related Questions