1

I have a text file with this ending:

line1^I$^M$
line2^I$^M$
line3^I$^M$

I have trying many options I found in the web by using sed command and even 'dos2unix' but they only work to remove the ^M$ but not ^I. So my file is now like this:

line1^I$
line2^I$
line3^I$

I have scrutinized in the web but there is nothing about ^I. What exactly does it mean?

  • 1
    ^I is a literal tab character. – DopeGhoti Jun 14 '18 at 21:21
  • How are you producing that output? If it's cat -A, how do you have 2 line ending $ chars in the output? – glenn jackman Jun 15 '18 at 13:39
  • A note to readers, use printf "line%d\t\r\n" 1 2 3 > file to create this file. – glenn jackman Jun 15 '18 at 13:39
2

So your file will look like this using od or cat -A

$ od -c foo
0000000   l   i   n   e   1  \t  \n   l   i   n   e   2  \t  \n   l   i
0000020   n   e   3  \t  \n
0000025
$ cat -A foo
line1^I$
line2^I$
line3^I$
$

To strip all ^I (Tab) characters, you could use sed -i 's/\t//g' foo. To only strip tabs at the end of a line, used sed -i 's/\t$//' foo

$ sed -i 's/\t//g' foo
$ od -c foo
0000000   l   i   n   e   1  \n   l   i   n   e   2  \n   l   i   n   e
0000020   3  \n
0000022
$ cat -A foo
line1$
line2$
line3$
$
  • 2
    To my knowledge, \t for TAB is only supported by GNU sed. Better use a literal tab instead (usually entered by preceeding it with ctrl-V). This should work anywhere. – Philippos Jun 15 '18 at 8:23
0

^I is probably not indicating a caret followed by an I, but rather is a representation of the character composed by pressing Ctrl+I. This character actually has a shortcut on most keyboards, to wit Tab. If you were to cat the file, you would probably see something like:

line1    $
line2    $
line3    $

Unless, that is, whatever you're using that is displaying literal ^Is is also showing the end of the line with a visible $.

You can use tr to eliminate them if you like, though:

$ cat file | tr -d '\t'

\t in this case is a magic sequence that is an easy way to represent a Tab character in several common tools (including, helpfully, tr).

dos2unix did nothing to the Tabs because they are identical between all the platforms that tool is used to "translate" to and from.

  • Thanks for your answer, I have tried cat file | tr -d '\t' but the ^I is still there, I can see it when I use cat -A file.txt – Fersal Jun 14 '18 at 21:46
  • That's because cat -A is what you use to make tabs and EOLs visible. The cat command will replace the contents of the file and display it to you. To make a new file without tabs, use cat file | tr -d '\t' > newfile. – DopeGhoti Jun 14 '18 at 21:48
  • Ooh! I got it, that is in the case to make a new file. I didn't know ^I is another form to represent a tab. Knowing that, now I have tried with sed as @steve suggested and it finally gone. Thanks – Fersal Jun 14 '18 at 22:02

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.