Remove spaces in the beginning of a string

Hi,

I am trying to remove spaces from the beginning of a string (which i am using in my shell script). For ex - my string looks like this -

" no rows selected"

and i want the string to look like this -

"no rows selected"

How can i achieve this?

Thanks!

echo " no rows selected" |sed 's/ no/no/g' 

Hi, I don't understand what you need.

do you have in your script " no rows selected"? but...a variable contains that or is hardcoded?..

@curleb - I have stored my string with leading spaces in a variable and when i try to use the command provided by you above...it returns a blank....i mean it does not return an output at all -

value=" no rows selected"
val=$value|sed 's/ no/no/g'
echo output=$val

This returns output as follows-

output=

which necessarily means its returning a blank output.

Any suggestions?

Thanks!!

Try this:

unix.com$ echo '   abc def' | sed 's/^\ *//'
abc def

Maybe it can help you a bit :slight_smile:

The same with a variable:

unix.com$ a='   abc def'
unix.com$ b=$(echo "$a" | sed 's/^\ *//')
unix.com$ echo "$b"
abc def

you're not actually seeing that the val assignment is erroroneous...you'll need to wrap it somehow, normally with $( ... ) in ksh:

value=" no rows selected"
val=$(echo $value |sed 's/ no/no/g' ) 
echo output=$val
1 Like

@tukuyomi - when i try the code provided by you...as follows -

value=" no rows selected"
val=$(echo "$value" | sed 's/^\ *//g')
echo output="$val"

I get the response as follows:

output=

no rows selected

So basically its adding an entire line of space in the output.

Any thoughts?

I would go with tukuyomi's solution, it takes care of string variables with spaces and it is more generic.

$>a='   abc def'; echo $(echo "$a" | sed 's/^\ *//')
abc def

---------- Post updated at 01:56 PM ---------- Previous update was at 01:53 PM ----------

I just tried, your code is working fine on my end..

$> value=" no rows selected"; val=$(echo "$value" | sed 's/^\ *//g'); echo output="$val"
output=no rows selected

try this, and see if you have access to sed command.. double check for typos..

$> which sed
/usr/bin/sed

@curleb - Thanks a ton! The command provided by you above worked like a charm.

I have a question though - How do I know if i have to enclose my command in $(...)? The way commands are to be used - is it shell specific?

Thanks!
Shruti

#!/usr/bin/ksh
value=" no rows selected"
val=$(echo "$value" | sed 's/^\ *//g')
echo output="$val"

$> test5.sh
output=no rows selected

Hello,
which shell are you using? can you post the entire file?

I have tried that with ksh and bash and it's ok

$ x=" no rows selected"
$ echo ${x# *}
no rows selected

(using Bash) It can be a good solution:

a='   abc def'; echo $a; echo "$a"

The first echo removes leading spaces while the second doesn't

Thanks all for your valuable suggestions. I was able to remove the leading spaces by using the following code provided by curleb -

val=$(echo $value |sed 's/ no/no/g' )

Thanks once again!

That gives the desired result with that specific value of $x, but it is generally wrong.

The parameter expansion pattern "# *" will only match one leading space, not more. The use of a single "#" means that the wildcard is useless; the shortest match will be selected and that match will always be a single space, if any. If you had used "##", it would've matched all characters (including non-spaces) that followed a leading space, effectively deleting the entire string. Remember, in sh globs, * is not a repeater that acts on the previous character, as it is in regular expressions; it can match an arbitrary length of arbitrary characters.

Also, the unquoted parameter expansion will convert any contiguous sequences of IFS characters (by default, spaces, tabs, and newlines) into a single space. Nothing in the problem statement indicates that such a conversion is desirable.

That is not a good solution. It removes more than just leading spaces. See above.

A proper pure posix-sh alternative:

while [ "$x" != "${x# }" ]; do
    x=${x# }
done

Regards,
Alister

Hi All,
A quick question -

How does the sed command actually works? I fail to understand its functionality.
For ex -

value= ' no rows selected'
val=$(echo $value |sed 's/ no/no/g' )
echo $val

sed removes the leading space from the string and returns the output as

no rows selected

But what if I want all the spaces removed from a string and the final output that i want is as follows

norowsselected

how do I achieve this?

val=$(echo $value |tr -d ' ' )
val=$(echo $value |sed 's/ //g' )

should work as well

Thanks this helped.