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?
# 8  
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
$ var="one two"

$ printf "%s" $var
onetwo

Note: with most of the versions of printf you'll not get the final new line.

Regards
Dimitre

Last edited by radoulov; 01-17-2007 at 04:42 AM..
# 9  
Old 01-17-2007
Quote:
Originally Posted by Perderabo
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


$ v="one two three four five six"

$ v=${v// /} && echo $v
onetwothreefourfivesix


Regards
Dimitre
# 10  
Old 01-17-2007
Dimetre,

Can you pls explain : v=${v// /}
# 11  
Old 01-17-2007
Quote:
Originally Posted by tayyabq8
Dimetre,

Can you pls explain : v=${v// /}
Variable Expansion

${variable//pattern1/pattern2} replace all occurrences of pattern1 with pattern2 in variable


Regards
Dimitre
# 12  
Old 01-17-2007
Dimitre, Thanks for the explanation.

Perderabo, Can you pls explain this condition construct also: [[ $v == *\ * ]]

Regards,
Tayyab
# 13  
Old 01-17-2007
Quote:
Originally Posted by tayyabq8
Dimitre, Thanks for the explanation.

Perderabo, Can you pls explain this condition construct also: [[ $v == *\ * ]]

Regards,
Tayyab
See Perderabo's last reply in https://www.unix.com/shell-programming-and-scripting/16942-string-extraction-user-input-sh.html
# 14  
Old 01-17-2007
Quote:
Got that, thanks Vino.
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