Creative Commons License
This blog by Tommy Tang is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

My github papge

Thursday, September 26, 2013

Three ways to change a space delimited file to a tab delimited file

Let's create a space delimited file:

tommy@tommy-ThinkPad-T420:~$ cat > new.txt
chr1 100 200
chr2 300 400
chr3 240 700

ctrl + D to indicate cat that this is the last line.

use awk:

tommy@tommy-ThinkPad-T420:~$ cat new.txt | awk -F ' ' '{print $1"\t"$2"\t"$3}'
chr1    100 200
chr2    300 400
chr3    240 700


use sed:

tommy@tommy-ThinkPad-T420:~$ cat new.txt | sed 's/ /\t/g'   
chr1    100  200
chr2    300 400
chr3    240 700

use tr :
tommy@tommy-ThinkPad-T420:~$ cat new.txt | tr ' ' '\t'   
chr1    100 200
chr2 300 400
chr3 240 700

tommy@tommy-ThinkPad-T420:~$ rm new.txt

you can redirect the newly generated tab delimited file to a new file
tommy@tommy-ThinkPad-T420:~$ cat new.txt | tr ' ' '\t' > new1.txt

Linux commands are so cool!


No comments:

Post a Comment