Remove spaces in the beginning of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove spaces in the beginning of a string
# 15  
Old 05-07-2010
Quote:
Originally Posted by anbu23
Code:
$ x=" no rows selected"
$ echo ${x# *}
no rows selected

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.


Quote:
Originally Posted by tukuyomi
(using Bash) It can be a good solution:
Code:
a='   abc def'; echo $a; echo "$a"

The first echo removes leading spaces while the second doesn't
That is not a good solution. It removes more than just leading spaces. See above.


A proper pure posix-sh alternative:
Code:
while [ "$x" != "${x# }" ]; do
    x=${x# }
done

Regards,
Alister

Last edited by alister; 05-07-2010 at 05:13 PM..
# 16  
Old 05-27-2010
Hi All,
A quick question -

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

Code:
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
Code:
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

Code:
norowsselected

how do I achieve this?
# 17  
Old 05-27-2010
Code:
val=$(echo $value |tr -d ' ' )

# 18  
Old 05-27-2010
Quote:
Originally Posted by shrutihardas
But what if I want all the spaces removed from a string and the final output that i want is as follows

Code:
norowsselected

how do I achieve this?
Code:
val=$(echo $value |sed 's/ //g' )

should work as well
# 19  
Old 05-27-2010
Thanks this helped.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove \n at the beginning of a field in a record.

Hi, In my file, I have few records which are split across multiple lines. File 1: ===== james,\n pre-auth completed,in patient,\n Fac_Id:23451,ramson,Dallas Expected is: ========== james,pre-auth completed,in patient,Fac_Id:23451,ramson,Dallas (8 Replies)
Discussion started by: machomaddy
8 Replies

2. Shell Programming and Scripting

remove all occurrences of a character at the beginning of a string

Hi there, i need some help to remove all occurrences of a certain character at the beginning of a string. Example: my string is 00102030 and i want to remove all zeros from beginning of string so the result is 102030 (3 Replies)
Discussion started by: gigagigosu
3 Replies

3. Shell Programming and Scripting

Remove filenames beginning with multiple dots

hi all, I want to remove filenames beginning with multiple dots.how I can do this. Thanks in advance (5 Replies)
Discussion started by: sriharsharavi
5 Replies

4. Shell Programming and Scripting

How to remove white spaces from the beginning an end of a string in unix?

Suppose, I have a variable var=" name is ". I want to remove the blank spaces from the begining and endonly, not from the entire string. So, that the variable/string looks like following var="name is". Please look after the issue. (3 Replies)
Discussion started by: mady135
3 Replies

5. Shell Programming and Scripting

How to remove extra spaces from a string??

Hi, I have a string like this and i want to remove extra spaces that exists between the words. Here is the sentence. $string="The small DNA genome of hepadnaviruses is replicated by reverse transcription via an RNA intermediate. This RNA "pregenome" contains ... (2 Replies)
Discussion started by: vanitham
2 Replies

6. Shell Programming and Scripting

sed - remove spaces before 1rst occurence of string

seems easy but havent found in other posts... i want to delete any spaces if found before first occurence of ${AI_RUN} sed 's/ *\\$\\{AI_RUN\\}/\\$\\{AI_RUN\\}/' $HOME/temp1.dat i think i'm close but can't put my finger on it. :rolleyes: (6 Replies)
Discussion started by: danmauer
6 Replies

7. Shell Programming and Scripting

sh, ksh: command to remove front spaces from a string?

dear pro-coders, is there any command out there that takes out the front spaces from a string? sample strings: 4 members 5 members 3 members but it has to be like so: 4 members 5 members 3 members (3 Replies)
Discussion started by: pseudocoder
3 Replies

8. Shell Programming and Scripting

remove blank spaces in a string

can any help how to remove blank spaces in a string? STR="GOOD BYE" by removing blank spaces, the string should be GOOD,BYE thanks in advance (2 Replies)
Discussion started by: spandu
2 Replies

9. Shell Programming and Scripting

Variable has spaces around the string, need to remove them

Hi all, I'm a newbie to the Linux world and I got a couple of shell script questions: (1) How do combine two variables and make it equal to a third variable? For example, I got a variable $A=FirstName, $B=LastName, and I want to combine the variable into one variable so when you echo the final... (4 Replies)
Discussion started by: mikey20
4 Replies

10. Shell Programming and Scripting

how to remove spaces in a string using sed.

Hello, I have the following to remove spaces from beginning and end of a string. infile=`echo "$infilename" | sed 's/^ *//;s/ *$//` How do I modify the above code to remove spaces from beginning, end and in the middle of the string also. ex: ... (4 Replies)
Discussion started by: radhika
4 Replies
Login or Register to Ask a Question