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

Originally published on my old Blogger blog on September 26, 2013. Read the original post and its comments.

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!

Related

Next
Previous
comments powered by Disqus