delete spaces in the variable in unix script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete spaces in the variable in unix script?
# 1  
Old 01-16-2007
delete spaces in the variable in unix script?

Hi All, I need your help.I want to know how to delete the spaces in a variable in unix scripting.Please give solution to this probelm...
thanks ! Smilie
# 2  
Old 01-16-2007
Code:
sun:/home/# var="test a string"
sun:/home/# echo $var | sed -e "s/ //g"
testastring

# 3  
Old 01-17-2007
Quote:
Originally Posted by MARY76
Hi All, I need your help.I want to know how to delete the spaces in a variable in unix scripting.Please give solution to this probelm...
thanks ! Smilie
With bash or ksh93:
Code:
$var="Test String"
$var=${var/ /}
$echo $var
TestString

# 4  
Old 01-17-2007
Code:
var=$( echo "$var" | tr -d ' ' )

or
Code:
var=$( echo "$var" | sed "s/ //g" )

# 5  
Old 01-17-2007
Unless you are using sh, go with tayyabq8's suggestion. It is more efficient than the others.
# 6  
Old 01-17-2007
but it did not work if there's another word (or have i missed something): eg

Code:
sun:/home/# var="Test String here"
sun:/home/# var=${var/ /}
sun:/home/# echo $var
TestString here

i am using bash.
# 7  
Old 01-17-2007
Quote:
Originally Posted by tayyabq8
With bash or ksh93:
Code:
$var="Test String"
$var=${var/ /}
$echo $var
TestString

That syntax only removes one space. But you could loop:
Code:
bash-3.00$ v="one two three"
bash-3.00$ v=${v/ /}
bash-3.00$ echo $v
onetwo three
bash-3.00$ v="one two three"
bash-3.00$ while [[ $v == *\ * ]] ; do v=${v/ /} ; done
bash-3.00$ echo $v
onetwothree
bash-3.00$

In any version of ksh you can use some ugly syntax to remove the first or last space. And again you can loop to remove all spaces...
Code:
$ v="one two three"
$ v=${v%${v##+([! ])}}${v#${v%${v##+([! ])}} }
$ echo $v
onetwo three
$ v="one two three"
$ v=${v% ${v#${v%%+([! ])}}}${v#${v%%+([! ])}}
$ echo $v
one twothree
$
$
$ v="one two three"
$ while [[ $v == *\ * ]] ; do v=${v%${v##+([! ])}}${v#${v%${v##+([! ])}} } ; done
$ echo $v
onetwothree
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove leading and trailing spaces for variable in shell script?

Hi I have variable named tablename. The value to tablename variable has leading and trailing white spaces. How to remove the leading and training white spaces and write the value of the tablename without space to a file using shell script. ( for e.g. tablename= yyy ) INPUT ... (10 Replies)
Discussion started by: pottic
10 Replies

2. Shell Programming and Scripting

How to get a numeric value from Oracle to UNIX variable without spaces?

Hi, I am using the below code to get a numeric value from oracle to unix variable: BD_RC_CNT=`sqlplus -s ${WMD_DM_CONNECT} <<EOF set heading off set pagesize 0 Select count(*) from wmd_bad_data where proc_id = ${PROC_ID} and file_id = ${FILE_ID} and file_dt =... (7 Replies)
Discussion started by: Arun Mishra
7 Replies

3. Shell Programming and Scripting

How to remove spaces between the columns in UNIX script?.

Hi guru's, I am trying to write a script to generate a csv file by connecting to database run a query and put the values into csv file. But the problem i face is i am getting lot of space after one value.how can i remove those values?. Please help. #!/bin/bash export... (2 Replies)
Discussion started by: karingulanagara
2 Replies

4. Shell Programming and Scripting

No delete black spaces!

Hi, I have the next problem, i am triying to concatenate two variables with white spaces at the cornes, but the shell deletes them. For example i have the next code: A="Hello " B="Hello" echo $A$B output: Hello Hello You can see only one space between the words, and i put 5... (5 Replies)
Discussion started by: Xedrox
5 Replies

5. Shell Programming and Scripting

trim spaces in unix for variable

HI Guys I have written a script using awk to split a file based on some identifier and renaming the file based on two values from specific length. ts a fixed width file. When I am trying to fetch the values a = substr($0,11,10) b = substr($0,21,5); i am getting spaces in a and b values .... (6 Replies)
Discussion started by: manish8484
6 Replies

6. Shell Programming and Scripting

Remove spaces / tabs from variable in script

I want to remove extra spaces from variable in aix script. We retrieve the data from oracle database and then print the values. We have a value on 90th position. When we execute the query on sqlplus it shows the length of 90th position as 3, but when we use the same query in aix script it shows... (5 Replies)
Discussion started by: lodhi1978
5 Replies

7. Shell Programming and Scripting

How can I stop the unix script from trimming extra spaces?

I have a file which contains certain records about users. the row length is always fixed to 205 characters. Now I want to read each record line from the file, substring some portion out of it and put into another file. But I have observed that my script is trimming the extra spaces I have used for... (4 Replies)
Discussion started by: Pramit
4 Replies

8. Shell Programming and Scripting

Delete spaces in between fields

I am new to unix and need some assistance. I have a file in the format below with about 15 fields per each record. I have 2 records displayed below. "1234","Andy ","Rich ","0001","123 Main Street ","Dallas " "2345","Andrew ","Richter ","0002","234 First Ave ... (12 Replies)
Discussion started by: guiguy
12 Replies

9. Shell Programming and Scripting

delete white spaces

hi all... i have the next question: i have a flat file with a lot of records (lines). Each record has 10 fields, which are separated by pipe (|). My problem is what sometimes, in the first record, there are white spaces (no values, nothing) in the beginning of the record, like this: ws ws... (2 Replies)
Discussion started by: DebianJ
2 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question