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
# 8  
Old 05-07-2010
I would go with tukuyomi's solution, it takes care of string variables with spaces and it is more generic.

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

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

Code:
$> which sed
/usr/bin/sed

# 9  
Old 05-07-2010
@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
# 10  
Old 05-07-2010
Code:
#!/usr/bin/ksh
value=" no rows selected"
val=$(echo "$value" | sed 's/^\ *//g')
echo output="$val"

$> test5.sh
output=no rows selected

# 11  
Old 05-07-2010
Hello,
which shell are you using? can you post the entire file?

I have tried that with ksh and bash and it's ok
# 12  
Old 05-07-2010
Code:
$ x=" no rows selected"
$ echo ${x# *}
no rows selected

# 13  
Old 05-07-2010
Quote:
Originally Posted by anbu23
Code:
$ x=" no rows selected"
$ echo ${x# *}
no rows selected

(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
# 14  
Old 05-07-2010
Thanks all for your valuable suggestions. I was able to remove the leading spaces by using the following code provided by curleb -

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

Thanks once again!
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