Search code examples
awksedtclregsub

Removing tabs from a string using either sed or awk and tcl regsub function


Say I have a string as name = xyz here after name there is a tab and after '=' there are random number of spaces.

I need an output as name=xyz

So how can I remove both tabs and random number of spaces from the above mentioned string using sed or awk and tcl regsub function. Help is highly appreciated. Thanks.

I tried

[regsub -all { +} $str ""]

This removes the random number of spaces from $str but tabs are still present.


Solution

  • Here is also solution using sed

    myvar=$(sed -E 's/\s*=\s*/=/g' <<<$myvar)
    

    or, to be more portable you can use [[:space:]] instead of \s

    myvar=$(sed -E 's/[[:space:]]*=[[:space:]]*/=/g' <<<$myvar)
    

    Here every occurrence of = surrounded by any number of whitespace symbols will be replaces with just =, without any whitespaces.