0

I have a tab delimited file, but in a few records, I see multiple TABS in between data

Sample Data: M^I^I^I^IJaghoub (^I is tab delimiter in cat -vet output) Required Output: MJaghoub

I have used sed and tr command but was not successful.

Please advise how to remove multiple TABS only.

  • Do you only want to remove all instances of multiple tabs and leave the instances of single tabs alone? – Nasir Riley May 2 at 20:08
  • @NasirRiley --you are correct, yes I need to remove all instances of multiple tabs and leave the instances of single tabs – Yarlly May 2 at 21:01
0
sed 's|[\t][\t][\t]*||g' file.txt

That will remove any instance of two or more tabs but will not affect any instances of single tabs.

If you've confirmed that you have the desired output and you want to edit the file in place, you can add -i'

sed -i 's|[\t][\t][\t]*||g' file.txt

I've tested this with a file that has instances of 1,2,3,4 and 5 tabs it removed instances of multiple tabs while leaving instances of single tabs alone. It effectively removes any instance of two tabs with 0 or more tabs coming afterwards.

To confirm, I added some more lines with instances of multiple tabs and single tabs to what you have in your question:

The file with cat -vet (Don't worry about the $, it just represents a line ending. If you use cat -vT, it doesn't have the $ for the line ending:

M^I^I^I^I^IJaghoub$
askdjfkasdf^I^Iksajk^I^Ijklsajkd$
klasjdfj^I^I^Ikljkasjdf^Ijaljkdsf$
asdkfj^I^I^Iklkljasdf^I^Ilkjkasdf$
sadfjkl^Ikjljas^Ikkjk^Ilkjlkj$

The output:

MJaghoub$
askdjfkasdfksajkjklsajkd$
klasjdfjkljkasjdf^Ijaljkdsf$
asdkfjklkljasdfkjkasdf$
sadfjkl^Ikjljas^Ikkjk^Ilkjlkj$

As you can see, it only removed instances of multiple tabs. The single tabs are still there.

  • thank you, Nasir – Yarlly May 6 at 12:34
2
tr -s '\t' <file >newfile

should squeeze multiple tab characters occurring next to each other into single tabs in file and write the modified contents to newfile.

Change -s to -d to delete all tabs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.