9

I'm stuck in including regular expressions with a sed command.

Q: I want to replace all occurences of two spaces after the end of a sentence with just once space.

Here is what I did:

sed 's/^ $/^$/' file  

And it didn't substituted two spaces with a one space after the sentence ends.

Output I get:

This is the output.  Hello Hello

Output I want:

This is the output. Hello Hello
4
  • @Rahul I want the output with that contains once space after the sentence ends. Not two spaces as in first sentence. I want the second sentence output
    – Zeus
    Jul 22, 2016 at 5:36
  • The answer by @Jasen is correct. BTW, the command you tried means: Substitute a line consisting of only a single whitspace by an empty line. This is why it did not do what you wanted. Jul 22, 2016 at 8:35
  • 1
    Insert here: war on single vs. double space after full stop
    – gerrit
    Jul 22, 2016 at 17:22
  • @gerrit That war was lost some 20 years ago. ;)
    – h4ckNinja
    Jul 23, 2016 at 6:50

3 Answers 3

13

Your sed command 's/^ $/^$/' won't do what you want. It just replace all lines contains one space with a line contain ^$.

Depend on what characters mark end of sentence, you can do:

sed -e 's/\([.?!]\) \{2,\}/\1 /g' <file

This will replace 2 or more spaces after ., ? or ! with one space only.

7
  • 1
    can be shortened to sed -r 's/([.?!])\s+/\1 /g' file
    – Sundeep
    Jul 22, 2016 at 5:52
  • Thanks. But would you kindly elaborate why so many round, square brackets you used? It's little confusing.
    – Zeus
    Jul 22, 2016 at 6:03
  • 1
    @spasic: That's work with GNU sed only, updated with simplified version.
    – cuonglm
    Jul 22, 2016 at 6:05
  • @Zeus: That's standard syntax with default BRE.
    – cuonglm
    Jul 22, 2016 at 6:05
  • 1
    No, the original sed editing script will replace any line containing a single space with the literal string ^$.
    – Kusalananda
    Jul 22, 2016 at 6:47
12
 sed 's/\.   */. /g' < file

replace dot followed by two or more spaces with dot followed by a single space.

5
  • Your command actually works for the whole paragraph. That's exactly what I wanted. Thanks!
    – Zeus
    Jul 22, 2016 at 5:59
  • or sed -r 's/\.\s+/. /g' file with GNU sed...
    – Sundeep
    Jul 22, 2016 at 6:08
  • @spasic use sed -E rather than sed -r. That works with GNU sed and *BSD sed (incl. Mac OS X) and some others. It's scheduled for inclusion as standard in POSIX "real soon now".
    – cas
    Jul 23, 2016 at 7:15
  • @cas I remember sometime back, I suggested sed -E to someone on this site which didn't work but sed -r worked.. I've worked with GNU sed alone, so no idea on POSIX and other variations.. will try to include this detail in future, thanks :)
    – Sundeep
    Jul 23, 2016 at 7:36
  • It must have been an old version of GNU sed. They've supported -E for quite a while now (since at least 2011, but the man and info pages still only mention -r)
    – cas
    Jul 23, 2016 at 9:12
7

This is what you might be looking for,

tr -s " " <filename

Sample,

$ echo "This is the output.  Hello Hello" | tr -s "[:blank:]"
This is the output. Hello Hello

Using sed,

$ echo "This is the output.  Hello Hello" | sed 's/\. \+/. /g'
$ echo "This is the output.  Hello Hello" | sed 's/\. \{1,\}/. /g'
This is the output. Hello Hello
3
  • 2
    Well, it is working thanks, but I need to include the sed command. Kindly tell something similar to above shown, like substitution, change text etc in sed.
    – Zeus
    Jul 22, 2016 at 5:38
  • 2
    I wasn't aware of this tr feature,
    – Jasen
    Jul 22, 2016 at 5:41
  • 3
    This approach will replace two spaces which are not end of sentence, too.
    – cuonglm
    Jul 22, 2016 at 5:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .