LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-23-2010, 01:30 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
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.
 
Old 06-23-2010, 01:37 AM   #2
Blender3D
Member
 
Registered: Jun 2010
Distribution: Linux Mint 9 x64, Linux From Scratch
Posts: 46

Rep: Reputation: 17
Are you using PERL? It is installed in virtually every single Linux distro (even Linux From Scratch!).

If so, just use:
Code:
sub trim
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
Make sure to flag the PERL interpreter in the top line:
Code:
#!/usr/bin/perl

Last edited by Blender3D; 06-23-2010 at 01:39 AM.
 
Old 06-23-2010, 01:39 AM   #3
Blender3D
Member
 
Registered: Jun 2010
Distribution: Linux Mint 9 x64, Linux From Scratch
Posts: 46

Rep: Reputation: 17
If that doesn't suit your needs, make a separate PERL file in the same folder the trims an argument string and returns a trimmed string (I'm not familiar with PERL, just with Google and a bit of Linux :P) when it is called.
 
Old 06-23-2010, 01:47 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Hi,

how about
Code:
echo $MACHINE_NAME | sed -r 's/(^ *)(.*[^ ])( *$)/\2/'
I could not test this since my bash already trims leading and trailing spaces all by itself. So please let me know if it works.

Last edited by crts; 06-23-2010 at 01:48 AM.
 
Old 06-23-2010, 01:50 AM   #5
Blender3D
Member
 
Registered: Jun 2010
Distribution: Linux Mint 9 x64, Linux From Scratch
Posts: 46

Rep: Reputation: 17
Okay, here's a simple trim.pl you can just call. Just run
Code:
trim.pl "    I Am Untrimmed!          "
And it outputs
Code:
I Am Untrimmed
I hope this suits your needs:
Code:
#!/usr/bin/perl

my $string = $ARGV[0];
$string =~ s/^\s+//;
$string =~ s/\s+$//;

print $string;
 
Old 06-23-2010, 01:51 AM   #6
Blender3D
Member
 
Registered: Jun 2010
Distribution: Linux Mint 9 x64, Linux From Scratch
Posts: 46

Rep: Reputation: 17
[facepalm]

I overkill everything...
 
Old 06-23-2010, 01:53 AM   #7
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 37
I have got following information from another forum.
The shell automatically removes seemingly excessive whitespaces, which is why you have to double quote variables if you want to preserve them.
Code:
MACHINE_NAME=$( echo $MACHINE_NAME )
This is so simple.
BTW thank you all for your quick replies.

Last edited by pinga123; 06-23-2010 at 01:54 AM.
 
Old 06-23-2010, 01:57 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by pinga123 View Post
I have got following information from another forum.
The shell automatically removes seemingly excessive whitespaces, which is why you have to double quote variables if you want to preserve them.
Code:
MACHINE_NAME=$( echo $MACHINE_NAME )
This is so simple.
BTW thank you all for your quick replies.
Hi,

which shell do you use? I tried single and double quoting the variable to preserve leading and trailing spaces. In both cases they were not preserved. I am now looking into shopt, maybe some options prevents it.
 
Old 06-23-2010, 02:04 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I wonder if it works with extended glob:
Code:
shopt -s extglob
MACHINE_NAME=${MACHINE_NAME#+( )}
MACHINE_NAME=${MACHINE_NAME%+( )}
Another way but not safe with IFS:
Code:
set -- $MACHINE_NAME
MACHINE_NAME=$1
And another way using read
Code:
read MACHINE_NAME __ <<< "$MACHINE_NAME"
# or
read __A MACHINE_NAME __B <<< "$MACHINE_NAME"
There probably should be a cleaner one.

Last edited by konsolebox; 06-23-2010 at 03:21 AM.
 
Old 06-23-2010, 03:11 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899
Personally I am curious how the original variable was created as bash would not allow the following:
Code:
MACHINE_NAME=    abcd
So perhaps instead of asking how to remove from an existing variable it should be how to remove whilst assigning.

Just a thought
 
Old 06-23-2010, 03:32 AM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by grail View Post
Personally I am curious how the original variable was created as bash would not allow the following:
Code:
MACHINE_NAME=    abcd
So perhaps instead of asking how to remove from an existing variable it should be how to remove whilst assigning.

Just a thought
Hi,

you can assign it by using quotes like
Code:
MACHINE_NAME="    abcd   "
However, when I do
Code:
echo $MACHINE_NAME
abcd
I can not preserve the trailing and leading spaces. So I do not see any need to remove them. I suppose the variable is being read from a file or so.
 
Old 06-23-2010, 10:56 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
It can be done in bash using:
  1. Shell parameter expansion's ${parameter##word} and ${parameter%%word} forms ...
  2. with pattern matching ...
  3. extended by enabling extended globbing (great name!) using the Shopt Builtin ...
  4. to allow the *(pattern-list) form "to match zero or more occurrences of the given patterns", in this case, zero or more occurrences of a space.
Here's how that looks at the command prompt:
Code:
c@CW8:~$ MACHINE_NAME='    ABCDEF  '           # Initialise
c@CW8:~$ shopt -s extglob                      # Enable extended globbing in pattern matching
c@CW8:~$ MACHINE_NAME=${MACHINE_NAME##*( )}    # Strip any and all spaces from the left
c@CW8:~$ echo "'$MACHINE_NAME'"                # Showing it has worked
'ABCDEF  '
c@CW8:~$ MACHINE_NAME=${MACHINE_NAME%%*( )}    # Strip any and all spaces from the right
c@CW8:~$ echo "'$MACHINE_NAME'"                # Showing it has worked
'ABCDEF'
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SSD TRIM support on Linux Thoreau Linux - Kernel 2 03-25-2009 05:05 AM
LXer: Tutorial: Custom Linux Kernels Trim Fat and Tune Performance LXer Syndicated Linux News 0 08-07-2007 12:01 PM
trim my box ekdya Fedora 6 09-25-2006 02:50 PM
How To Trim A File fpfernando Programming 11 01-05-2006 09:04 AM
I want to trim Linux distribution qadria Linux - Software 7 08-08-2005 08:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration