Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace whitespaces with tabs in linux

How do I replace whitespaces with tabs in linux in a given text file?

like image 509
biznez Avatar asked Sep 14 '09 21:09

biznez


People also ask

Which command will translate all whitespace to tabs?

The tr command in UNIX is a command line utility for translating or deleting characters.

How do I convert spaces to tabs in Vim?

vim Whitespace Convert tabs to spaces and spaces to tabs :retab! If you enable expandtab again :set expandtab then and run the :retab! command then all the tabs becomes spaces. If you want to do this for selected text then first select the text in visual mode.


2 Answers

Use the unexpand(1) program


UNEXPAND(1)                      User Commands                     UNEXPAND(1)  NAME        unexpand - convert spaces to tabs  SYNOPSIS        unexpand [OPTION]... [FILE]...  DESCRIPTION        Convert  blanks in each FILE to tabs, writing to standard output.  With        no FILE, or when FILE is -, read standard input.         Mandatory arguments to long options are  mandatory  for  short  options        too.         -a, --all               convert all blanks, instead of just initial blanks         --first-only               convert only leading sequences of blanks (overrides -a)         -t, --tabs=N               have tabs N characters apart instead of 8 (enables -a)         -t, --tabs=LIST               use comma separated LIST of tab positions (enables -a)         --help display this help and exit         --version               output version information and exit . . . STANDARDS        The expand and unexpand utilities conform to IEEE Std 1003.1-2001        (``POSIX.1''). 
like image 112
DigitalRoss Avatar answered Oct 05 '22 01:10

DigitalRoss


I think you can try with awk

awk -v OFS="\t" '$1=$1' file1 

or SED if you preffer

sed 's/[:blank:]+/,/g' thefile.txt > the_modified_copy.txt 

or even tr

tr -s '\t' < thefile.txt | tr '\t' ' ' > the_modified_copy.txt 

or a simplified version of the tr solution sugested by Sam Bisbee

tr ' ' \\t < someFile > someFile 
like image 41
Jonathan Avatar answered Oct 05 '22 01:10

Jonathan