Stripping the spaces in a string variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stripping the spaces in a string variable
# 1  
Old 03-27-2008
Stripping the spaces in a string variable

Hi ,
i have to strip the spaces in the string which has the following value
Code:
    ABC                           DEF

i want this to appear like this
Code:
ABC DEF

is there any spilt method?
please help....


Thanks
# 2  
Old 03-27-2008
man tr, in particular the -s option

Also if you have a value in a variable, simply echoing it will normalize the spacing if you don't quote it (but this has other risks if the string value might contain special characters).
# 3  
Old 03-27-2008
in Perl:
Code:
my $str = "ABC                           DEF";
$str =~ s/\s{2,}/ /g;    # substitute two or more spaces with a single one, globally

# 4  
Old 03-27-2008
in vi mode

:%s/ / /g

essentially every 2 spaces in the file change to 1 space
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking variable with specific string and stripping number from it.

I am parsing a file and I get differnt results everytime. Sometimes I get 12s sometimes I get 54m and sometime 3h.. v1=12s or v1=54m or v1=3h 12s - 12 seconds 54m - 54 minutes 3h - 3 hour I have to write a script in such a way that it whenever v1 is in minutes, I should strip "m"... (14 Replies)
Discussion started by: jayeshpatel
14 Replies

2. Shell Programming and Scripting

append blank spaces at the end of a variable string

Hello, could you please help with this one. I have an input file like this: 123,4567,89000 123456789,9876543,12 and for the output I need strings to be with the fixed length, let's say 15, and if the string is -lt 15 to be populated with blanks at the end until it reach 15, like this: 123 ,4567... (1 Reply)
Discussion started by: apenkov
1 Replies

3. Shell Programming and Scripting

Stripping characters from a variable

I'm using a shell script to get user input with this command: read UserInput I would then like to take the "UserInput" variable and strip out all of the following characters, regardless of where they appear in the variable or how many occurrences there are: \/":|<>+=;,?*@ I'm not sure... (5 Replies)
Discussion started by: nrogers64
5 Replies

4. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

5. Shell Programming and Scripting

stripping a variable down

I have a variable that has an absolute path for a file on my computer. This dynamically changes. Is there a way I can assign two new variables from that one? variable: /Users/keith/Desktop/test/file.mov 1) filename - no path or extention ....so just....file 2) path no filename or... (3 Replies)
Discussion started by: mainegate
3 Replies

6. Shell Programming and Scripting

stripping leftmost characters from string

Hi there, if i have some strings ie test_324423 test_242332 test_767667 but I only want the number part (the bolded bit) how do I strip the leftmost 5 characters from the output so that i will have just 324423 242332 767667 any help would be greatly appreciated Gary (5 Replies)
Discussion started by: hcclnoodles
5 Replies

7. UNIX for Dummies Questions & Answers

Stripping a portion of string from behind!!!

Hi, How to strip a portion of a file name from behind...Say for Eg..i have a file name like aaaaa.bbbbb.Mar-17-2007 i want to remove .Mar-17-2007...is there a one line command which can give this output... Thanks Kumar (5 Replies)
Discussion started by: kumarsaravana_s
5 Replies

8. UNIX for Advanced & Expert Users

Please Help!! Reading a string of text with concurrent spaces into a shell variable

Please Help!! Here is a very simplistic example of what I am trying to accomplish. I need what I have inbetween the quotes to be read into the shell variable. x="This is fun" echo $x The results of x from the above expression is: This is fun Notice the unix takes out the... (1 Reply)
Discussion started by: mjs3221
1 Replies

9. UNIX for Dummies Questions & Answers

Stripping leading spaces on right justified name

I have a name field in a file which is right justified (yep - its true). I need to strip the leading spaces from the field and write the name out left justified. Again, I think I need to use a sed or awk command but so far, my results are at best disappointing. Thank you in advance from a UNIX... (2 Replies)
Discussion started by: Marcia P
2 Replies

10. 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
Login or Register to Ask a Question