How to trim variable in linux?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to trim variable in linux?
# 1  
Old 06-23-2010
How to trim variable in linux?

Hi guys ,
I m developing a script in linux.but getting confused about how would i trim my variable if there is any space.

for example.
I m storing value in a variable named MACHINE_NAME
if MACHINE_NAME=<space><space><space>ABCDF<space><space>
How would i trim it to have my MACHINE_NAME=ABCDF (with no spaces in left or right side.
# 2  
Old 06-23-2010
Code:
MACHINE_NAME=$( echo $MACHINE_NAME )

The shell automatically removes seemingly excessive whitespaces, which is why you have to double quote variables if you want to preserve them.
This User Gave Thanks to pludi For This Post:
# 3  
Old 06-23-2010
Code:
# echo "$MACHINE_NAME"|sed 's/ //g'

Code:
# echo $(echo "$MACHINE_NAME")

# 4  
Old 06-23-2010
I got is solved there was some mistake in spelling. Sorry pludi for inconvenience.

Last edited by pinga123; 06-23-2010 at 03:20 AM..
# 5  
Old 06-23-2010
Hi
Code:
VM_GUEST=$( echo $VM_GUEST )

The underscore for missing between VM and GUEST in the echo statement.

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 6  
Old 06-23-2010
Code:
VM_GUEST=$( echo $VM_GUEST )

You're missing an underscore here
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trim Space

In Shell, I have output of a unix command as test1 test2015 but I want it as test1 test2015 can anyone help me out. Use code tags, thanks. (3 Replies)
Discussion started by: OscarS
3 Replies

2. Shell Programming and Scripting

How to trim the zero's after decimal?

Hello all, I have an XML with below content from which i need to remove the trailing zeros, like 123.00 should be converted to 123 and 123.01200 to 123.012 Below is the sample excerpt data from XML file. My input file size could be approximately 5 GB or less. CURRENT:... (10 Replies)
Discussion started by: Ariean
10 Replies

3. Shell Programming and Scripting

Trim sed output & assign to variable

Hi, I have the following command that parses an xml file to read a node <port>'s value. Hoever the output comes with spaces. My requirement is to trim the spaces around the value and assign to a variable. sed -n 's|<port>\(.*\)</port>|\1|p' ../cfg.xml How do I go about it? (6 Replies)
Discussion started by: sai2013
6 Replies

4. Shell Programming and Scripting

Trim String

Hi, I wish to grep everything before the last "/bin" in the following string /opt/app/bin/app1/jdk150_07/bin/IA64N/java Desired output: "/opt/app/bin/app1/jdk150_07" Kindly help ... (2 Replies)
Discussion started by: mohtashims
2 Replies

5. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

6. 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

7. Shell Programming and Scripting

another trim question using tr

Hi all, I've been looking for how to eliminate blank spaces in a variable or strings. I've seen several ways, using sed, awk and even python. One of them is use 'tr' command, but it does not work as I expected: For example: echo " stuff " | tr -s " "leaves one space ahead and another... (3 Replies)
Discussion started by: AlbertGM
3 Replies

8. Shell Programming and Scripting

trim lines

I want to delete all lines from 1 till a certain line. how to do that without redirection to another file (tail solution)? thanx (6 Replies)
Discussion started by: melanie_pfefer
6 Replies

9. Shell Programming and Scripting

Trim

Hello, I am passing a filename to a script to draw parameters from it. However, I want to use part of the filename as a parameter. The filename is transfer_ccf_3731_10.sh but I only need the 3731_10 part of it. Is this possible? Any help or suggestions would be appreciated! Regards, J. (4 Replies)
Discussion started by: JWilliams
4 Replies

10. Shell Programming and Scripting

How to trim space in output variable ?

Hi , I have a code like this: uid=scott password=tiger database=db01 cat >runid_val.sql<<-EOA SET ECHO OFF SET FEEDBACK OFF SET HEADING OFF SELECT trim(runid_seq.nextval) FROM dual; EXIT EOA echo `cat runid_val.sql` V_RUNID=`sqlplus -s $uid/$password@$database @runid_val.sql`... (5 Replies)
Discussion started by: vj_76
5 Replies
Login or Register to Ask a Question