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
# 1  
Old 05-07-2010
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 -
Code:
" no rows selected"

and i want the string to look like this -
Code:
"no rows selected"

How can i achieve this?

Thanks!
# 2  
Old 05-07-2010
Code:
echo " no rows selected" |sed 's/ no/no/g'

# 3  
Old 05-07-2010
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?..
# 4  
Old 05-07-2010
@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 -

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

This returns output as follows-

Code:
output=

which necessarily means its returning a blank output.

Any suggestions?

Thanks!!
# 5  
Old 05-07-2010
Try this:
Code:
unix.com$ echo '   abc def' | sed 's/^\ *//'
abc def

Maybe it can help you a bit Smilie

The same with a variable:
Code:
unix.com$ a='   abc def'
unix.com$ b=$(echo "$a" | sed 's/^\ *//')
unix.com$ echo "$b"
abc def


Last edited by tukuyomi; 05-07-2010 at 02:47 PM..
# 6  
Old 05-07-2010
you're not actually seeing that the val assignment is erroroneous...you'll need to wrap it somehow, normally with $( ... ) in ksh:

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

This User Gave Thanks to curleb For This Post:
# 7  
Old 05-07-2010
@tukuyomi - when i try the code provided by you...as follows -

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

I get the response as follows:

Code:
output=

no rows selected

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

Any thoughts?
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