Need Quick help on Understanding sed Regex

Hi Guys,

Could you please kindly explain what exactly the below SED command will do ?

I am quite confused and i assumed that,

sed 's/[ \t]*$/ /'
  1. It will remove tab and extra spaces .. with single space.

The issue is if it is removing tab then it should be � right ..
please assist.

it looks like it's *trying* to remove tabs and spaces, with a single space.
the items in square brackets are the list of matches it'll look for, the "*" after the square bracket says "0 or more of these". the "$" is end of line.

Rest is pretty self explanatory (I hope) :slight_smile:

Honestly, though, I've never had much luck using sed for replacing control characters, I've had much better luck using the "tr" command.

ie:

echo "blah#blah" | tr -s "#" "\t" | tr -s "\t" "-"

(since I can't display a tab properly, I'll do a 2 step, convert to a tab, then from .. obviously you'd just use one or the other in practice :wink: )

It will remove trailing whitespace with a single space, but only if it is GNU sed. Regular sed does not understand \t , so then it will remove trailing spaces, backslashes and t's..

Regular sed

$ printf "%s\n" "Just wait      " | sed 's/[ \t]*$/ /'
Just wai 

GNU sed:

$ printf "%s\n" "Just wait      " | sed 's/[ \t]*$/ /'
Just wait 

Regular sed takes a hard TAB character (you can enter it with CTRL-V TAB)

printf "%s\t\t\n" "Just wait      " | sed 's/[       ]*$/ X/'
Just wait X

In the square brackets are a space and a TAB-character. This is also understood by GNU sed, so for portability use that instead of \t

I interpret the sed command as follows:

  1. If the line is a blank line, it will substitute any number of spaces and tabs with a single space
  2. If the line contains some text, it will substitute any number of trailing spaces and tabs with a single space

---------- Post updated at 07:33 PM ---------- Previous update was at 07:32 PM ----------

Oh, too late, again :rolleyes: