LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to trim variable in linux? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-trim-variable-in-linux-815821/)

pinga123 06-23-2010 12:30 AM

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.

Blender3D 06-23-2010 12:37 AM

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

Blender3D 06-23-2010 12:39 AM

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.

crts 06-23-2010 12:47 AM

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.

Blender3D 06-23-2010 12:50 AM

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;


Blender3D 06-23-2010 12:51 AM

[facepalm]

I overkill everything...

pinga123 06-23-2010 12:53 AM

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.

crts 06-23-2010 12:57 AM

Quote:

Originally Posted by pinga123 (Post 4012246)
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.

konsolebox 06-23-2010 01:04 AM

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.

grail 06-23-2010 02:11 AM

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

crts 06-23-2010 02:32 AM

Quote:

Originally Posted by grail (Post 4012298)
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.

catkin 06-23-2010 09:56 AM

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'



All times are GMT -5. The time now is 07:58 AM.