Skip to content
Prev 164180 / 398506 Next

Replacing tabs with appropriate number of spaces

This is basically your approach, but automated a bit more than you describe:


library(gsubfn)

tmp <- strsplit('one\ttwo\nthree\tfour\n12345678\t910\na\tbc\tdef\tghi\n','\n')[[1]]

tmp2 <- gsubfn('([^\t]+)\t', function(x) {
  ln <- nchar(x)
  nsp <- 8-(ln %% 8)
  sp <- paste( rep(' ', nsp), collapse='' )
  paste(x,sp, sep='')
}, tmp )

tmp2
cat(tmp2, sep='\n')

This is based on the assumption of tab stops every 8 columns, change the 2 8's above if you want something different.

Hope this helps,


--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111