Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I find that sed often fails to pattern-match whitespace. Consider the following examples:

echo 'A     B     C' | sed 's/[ \t]*//g'
ABC

echo -e 'A\tB\tC' | sed 's/[ \t]*//g'
A       B       C

perl -e 'print "A     B     C\n"' | sed 's/[ \t]*//g'
ABC

perl -e 'print "A\tB\tC\n"' | sed 's/[ \t]*//g'
A       B       C

Could someone please explain why sed sometimes fails to match whitespace?

share|improve this question
    
It works on my Ubuntu 11.10 / sed 4.2.1 like @Peter also said. What is the system you use? sed version? environment variables? – olibre Jan 21 '12 at 0:34
up vote 3 down vote accepted

1.

echo -e 'A\tB\tC' | sed 's/[ \t]*//g'
A       B       C

Works for me

$ echo -e 'A\tB\tC' | sed 's/[ \t]*//g'
ABC

Try this

$ echo -e 'a\tb' | sed -e 's/[ \t]//'
ab

2.

perl -e 'print "A     B     C\n"' | sed 's/^[ \t]*//g'
A       B       C

your string doesn't contain a space at the start (^) of the line

share|improve this answer
    
Thank you. I removed the stray ^ anchor. How else can I match a tab character? – user001 Jan 21 '12 at 0:23
    
Thank you. Removing the * multiplier (echo -e 'A\tB\tC' | sed 's/[ \t]//g') did not work. – user001 Jan 21 '12 at 0:26
    
@user001: echo -e 'A\tB\tC' | perl -pe 's/[ \t]*//g' :-) – RedGrittyBrick Jan 21 '12 at 0:32
    
Oh, that works wonderfully. Thank you so much. Any idea why the perl script works but the awk script fails? They appear identical. – user001 Jan 21 '12 at 0:35
    
@user001: I don't know why your sed doesn't work. It seems broken. Maybe something in the environment is triggering some feature. – RedGrittyBrick Jan 21 '12 at 0:37

Some versions of sed don't support \t and some other escapes on the left hand side of a substitution command.

You can find information on variations among versions of sed in the FAQ.

share|improve this answer

The second statement you have above works on my machine (Ubuntu 10.10 amd64, sed ver 4.2.1, sed package version 4.2.1-7) and I don't really have an explanation as to why that one wouldn't work.

The third and fourth are because you are using the beginning-of-line anchor (^), so it would only strip the leading whitespace and in your input text, you don't have any whitespace at the beginning of the lines.

share|improve this answer
    
I forgot to remove the BOL anchor (^). Even if it is removed, the fourth statement fails to work. Post amended. – user001 Jan 21 '12 at 0:20

You should use \s to match both whitespace and tab:

echo 'A     B     C' | sed 's/\s//g'
ABC

echo -e 'A\tB\tC' | sed 's/\s//g'
ABC

perl -e 'print "A     B     C\n"' | sed 's/\s//g'
ABC

perl -e 'print "A\tB\tC\n"' | sed 's/\s//g'
ABC
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .