5

I am trying to use regular expressions to remove all whitespaces after a specific word. The editor I am using is Notepad++ but am able to use Unix

In this case it is VALUES I'm able to highlight by running regex \VALUES but I am unsure how to remove whitespaces after this.

  • 5
    By "after", do you mean immediately after, or anywhere after? – Kusalananda Jan 23 '17 at 13:12
8

Using sed:

$ sed 's/VALUES[[:blank:]]*/VALUES/g' input >output

This will replace VALUES followed by any number of whitespace characters (space or tab) with VALUES, i.e. it will remove the whitespace.

If it's important that VALUES is matched as a complete word, i.e. that whateverVALUES is not matched, then insert a (beginning-of-)word boundary pattern before VALUES:

$ sed 's/[[:<:]]VALUES[[:blank:]]*/VALUES/g' input >output

I will leave that out for the remainder of this answer.

For a more complicated value of VALUES, it might be convenient to not have to type it twice:

$ sed 's/\(VALUES\)[[:blank:]]*/\1/g' input >output

This saves the VALUES pattern and reuses it in the replacement.

If the pattern VALUES is stored in the shell variable $values:

$ sed "s/\($values\)[[:blank:]]*/\1/g" input >output

This transfers more or less directly into the Vim editor:

:%s/\(VALUES\)[[:blank:]]*/\1/

As I've never used Notepad++, I can only guess how to use it. One should apparently be able to press Ctrl+H and enter a search/find and replace pattern.

The search pattern may be (VALUES)[[:blank:]]* while the replace pattern may be $1. I have no way of testing this, sorry. If [[:blank:]] doesn't work, try with [\t ].

|improve this answer|||||
  • This will replace the "VALUES" with "VALUES" even if there is not space after it, unnecessary replacement – Wissam Roujoulah Jan 23 '17 at 13:53
  • 4
    @MhdWissamAl-Roujoulah It will indeed. And it is unnecessary. But the extra time taken to figure out whether to use + or \+ in place for * is longer than the time taken to execute a few extra substitutions, and the end result will be identical. – Kusalananda Jan 23 '17 at 13:58
  • 2
    Also it might catch words ending in VALUES (though LVALUES is the only one I can thing of off the top of my head). – jamesqf Jan 23 '17 at 18:02
  • @jamesqf Now that's a good point that I didn't think of! Come back in five minutes and I will have fixed that. – Kusalananda Jan 23 '17 at 18:03
  • @Kusalananda: Thanks, I learned something new. Didn't know that there was a special beginning-of-word pattern. – jamesqf Jan 24 '17 at 4:38
3

You can use \s* (GNU) or [[:space:]]* (portable) to catch tabs (and all other horizontal and vertical spacing characters) as well as spaces.

$ cat file
stuff and VALUES    <--tab
more VALUES   <--three spaces
VALUES      <--tab, three spaces, tab

$ sed 's/VALUES\s*/VALUES/' file
stuff and VALUES<--tab
more VALUES<--three spaces
VALUES<--tab, three spaces, tab
|improve this answer|||||
  • 4
    Note that since \s or [[:space:]] match on the CR character, that could corrupt MS-DOS line delimiters. See [[:blank:]] for horizontal spacing characters only. – Stéphane Chazelas Jan 23 '17 at 13:20
2

Using sed:

sed -e 's/VALUES \+/VALUES/'

Using + to match all spaces after VALUES

|improve this answer|||||
0

In Notepad++ bring up the find and replace window

In the find box, enter (VALUES)\s* (this matches "VALUES" followed by any amount of whitespace) In the replace box, enter \1 (this insterts the first group (inside parenthesis ())

Ensure regular expressions are enabled

Click replace all to replace all instances of "VALUES[some whitespace]" with "VALUES"

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.