I am using a bash script that parses data from a bunch of different text files and generates a .dts file. The .dts file contains spaces between each column title and I need to remove them as it makes a plotting tool I use crash. See below for example:

Avg Interval Time           Interval Num        Total pts         Avg Delta Time

I need it to look like this (There are spaces between each column title)

AvgIntervalTime           IntervalNum        Totalpts         AvgDeltaTime

Can the sed command perform this function? The above is all in Line 1 of the .dts file.

FSA, I have done some research on the sed command and i can only find examples of how to remove leading/trailing edge spaces or all the spaces, but nothing to just remove spaces between certain words.

  • a) Are the spaces between column titles also blank characters or are they tabs? If they're blanks b) is each column a fixed width or not? c) is there guaranteed to be 2 or more blanks between titles and never more than 1 blank within a title?
    – Ed Morton
    Commented Feb 20 at 21:30

2 Answers 2


If you want to match a single space occurring between word characters, you can anchor it with \b:


Matches a word boundary; that is it matches if the character to the left is a “word” character and the character to the right is a “non-word” character, or vice-versa.

So for example

$ cat file
Avg Interval Time           Interval Num        Total pts         Avg Delta Time

$ sed 's/\b \b//g' file
AvgIntervalTime           IntervalNum        Totalpts         AvgDeltaTime

Here are a few scripts:

  • sed 's/\([^ ]\) \([^ ]\)/\1\2/g' When the words are separated by one space and the columns are separated by tabs or multiple spaces. Edit: Please see @steeldriver's answer for a better version of this script
  • sed '1s/\([^ ]\) \([^ ]\)/\1\2/g' First line only
  • tr -d ' ' Remove all spaces
  • sed 's/Avg \+Interval \+Time/AvgIntervalTime/;s/Interval \+Num/IntervalNum/;s/Total \+pts/Totalpts/;s/Avg \+Delta \+Time/AvgDeltaTime/' Manually specify the words and remove any number of spaces

Pipe the .dts file to the sed command, then redirect to a new file or use -i with sed <expr> <filename>


You must log in to answer this question.

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