Operating System - Linux
1819932 Members
3253 Online
109607 Solutions
New Discussion юеВ

Unalbe to remove "tab" character using "sed"

 
Raghu Chikkamenahalli
Frequent Advisor

Unalbe to remove "tab" character using "sed"

Hello,

Trying to remove a tab character using sed as follows.

sed 's/[ \t]*$//' file_name.txt

But it is not removing the tab space as expected. To remove the tab space, again ran the sed as follows.

sed 's/[ ]*$//' file_name.txt ( here insted of "\t" used the tab key .

Can anybody help to resolve this.

Regards,
Raghu.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Unalbe to remove "tab" character using "sed"

Hi:

Not all 'sed' variations support the '\t' escape character.

Using the TAB key as you did is valid.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Unable to remove "tab" character using "sed"

>JRF: Not all 'sed' variations support the '\t' escape character.

I don't see it documented in sed(1), or ed(1) or in regexp(5).

tr(1) mentions it takes \ then octal digits.

You could use a character class:
sed 's/[:space:]*$//' file_name.txt
Or:
$ sed -e "s/$(echo "[ \t]\c")\*$//" file_name.txt

James R. Ferguson
Acclaimed Contributor

Re: Unalbe to remove "tab" character using "sed"

Hi (again):

> Dennis: >JRF: Not all 'sed' variations support the '\t' escape character.

> Dennis: I don't see it documented in sed(1), or ed(1) or in regexp(5).

My remark was based both on experience and on the reference "...that the only C-like backslash sequences that you can portably assume to be interpreted are \n and \\; in particular \t is not portable, and matches a `t' under most implementations of sed, rather than a tab character."

...from:

http://www.gnu.org/software/sed/manual/sed.html

I too thought about suggesting the '[:space:]' character class, but rejected that since the author specifically asked about the '\t' escape and not the more generalized "whitespace" (space, tab, etc.).

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor

Re: Unable to remove "tab" character using "sed"

>JFF: My remark was based both on experience and on the reference

My comment about the documentation was that it didn't mention allowing it.

>I too thought about suggesting the '[:space:]' character class,

I suggested it since the above RE had a space AND a tab, so why not go for broke. :-)

Raghu Chikkamenahalli
Frequent Advisor

Re: Unalbe to remove "tab" character using "sed"

Hello JRF,
Thanks for addressing the problem. It helpmed to resolve the issue.
regards,
raghu.