Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replacing tabs with single tab in sed

Tags:

regex

windows

sed

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
like image 531
Nick Ginanto Avatar asked Aug 26 '13 06:08

Nick Ginanto


People also ask

How do you replace multiple spaces with commas in Unix?

sed s/ */ /g This will replace any number of spaces with a single space. sed s/ $// This will replace any single space at the end of the line with nothing. sed s/ /,/g This will replace any single space with a single comma.


1 Answers

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
like image 140
devnull Avatar answered Sep 18 '22 13:09

devnull