Friday, April 8, 2016

Multiple spaces/tabs to single tab - code

To convert multiple spaces and tabs to a single tab use the code below:

Input file.txt

chr1       10100           10330            
chr1     10345    10590                                 
chr1    16100       16315      
chr1    20060      20210   
chr1      56200   56350   
Code. First use tr -s to squeeze spaces, then sed to substitute spaces to tabs, then tr -s to squeeze tabs.

tr -s " " < file.txt > file.tmp
sed -i 's/\s/\t/g' file.tmp
tr -s "\t" < file.tmp > file.txt
Output file.txt

chr1    10100   10330
chr1    10345   10590
chr1    16100   16315
chr1    20060   20210
chr1    56200   56350

No comments:

Post a Comment