Wednesday, April 6, 2016

How to change multiple spaces to a single tab in Unix

If you copy/paste tab separated files in terminal, it may happen that tabs are changed into multiple spaces. In case you end up with a file with multiple spaces and you need them to be changed to a single tab use tr and sed commands, tr with -s option will squeeze multiple spaces and sed will substitute a space \s with a tab \t.

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

No comments:

Post a Comment