9

I want to replace all multiple tabs with a single tab with sed. I am trying to use

sed 's:\t+:\t:' .\text.CSV > newtext.csv

but this doesn't seem to work

If I open in sublime and replace by regex all \t+ to \t it works properly

what is wrong with my sed?

Also, if I replace tabs with a comma with

sed 's:\t\t*:,:g' text.CSV > newtext.csv

I get this kind of line

264262360,20030826,200308,2003,2003.6466,BUS,EMPLOYER,,,,,,BUS,,, ,,,,,,,,,,0,051,051,05,1,3.4,12,2,12,5.24866163479182,1
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244
  • 1
    Remember `+` means one or more so can also be written `\t\t*` – potong Aug 26 '13 at 06:33
  • If `\t` isn't working, try using [Ctrl]-[v] [Tab] ([see this answer](http://stackoverflow.com/questions/6392249/how-to-enter-a-tab-char-on-command-line)) if working in a a *nix terminal. Copy-pasting a tab character might work, but in my terminal tabs were replaced by four spaces. – Joel Purra Nov 05 '14 at 09:22

3 Answers3

20

You can also use tr to replace multiple tabs with a single one:

tr -s '\t' '\t' < inputfile > outfile

The -s option squeezes repeats:

-s, --squeeze-repeats

      replace each input sequence of a repeated character that is
      listed in SET1 with a single occurrence of that character
devnull
  • 118,548
  • 33
  • 236
  • 227
8

Use -r option and g flag:

sed -r 's:\t+:\t:g' text.CSV > newtext.csv

-r to make + to work.

g flag to replace all occurrences.

UPDATE

If your sed does not support -r option, try following instead:

sed 's:\t\t*:\t:g' text.CSV > newtext.csv
falsetru
  • 357,413
  • 63
  • 732
  • 636
0

you could also use

sed "s/\t\+/\t\g" test.csv >> newtest.csv

anim
  • 1