LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-28-2012, 03:53 AM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 30
Change variable


I have a script as below ,

FILE='awk -F: '{print $1, $6} /tmp/file.txt'

mv FILE /ora

however the FILE may be have space , can advise how to trim the variable ?

thanks.
 
Old 09-28-2012, 04:53 AM   #2
suttiwit
Member
 
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 192
Blog Entries: 2

Rep: Reputation: 22
Obviously, you need to do:
Code:
mv $FILE /ora
instead of:
Code:
mv FILE /ora
 
Old 09-28-2012, 05:49 AM   #3
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by suttiwit View Post
Obviously, you need to do:
Code:
mv $FILE /ora
instead of:
Code:
mv FILE /ora
Thanks , I missed to type $ in my question .

As my question , how to trim it ?

Thanks.
 
Old 09-28-2012, 06:00 AM   #4
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
Where could those spaces you mention be in the FILE variable?
If they can be in the middle, then you need to use double quotes (") to protect them:
Code:
mv "$FILE" /ora
If they can appear at the start or end of the string, then I guess Google should give you some insight
 
Old 09-28-2012, 06:07 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by ust View Post
I have a script as below ,

FILE='awk -F: '{print $1, $6} /tmp/file.txt'

mv FILE /ora

however the FILE may be have space , can advise how to trim the variable ?

thanks.
It is not clear what you are attempting to do and where this space might come from.

This for example will add a space between field 1 and 6:
Code:
awk -F: '{print $1, $6} /tmp/file.txt
This will not add a space:
Code:
awk -F: '{print $1 $6} /tmp/file.txt
Please elaborate before all of us start guessing.
 
Old 09-28-2012, 12:45 PM   #6
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Rep: Reputation: 2
Code:
FILE=$(awk -F: '{print $1, $6}' /tmp/file.txt | sed "s: ::g")
mv $FILE /ora
I am sure you are missing a quote somewhere in your previous post, please make sure that you post EXACTLY what you have to ensure that we have a better view of what you are trying to achieve here.
Ted

Last edited by ted_chou12; 09-28-2012 at 01:37 PM.
 
Old 09-28-2012, 01:26 PM   #7
evgenyz
Member
 
Registered: Sep 2012
Posts: 48

Rep: Reputation: Disabled
Try the following:
FILE=`awk -F: '{print $1, $6}' /tmp/file.txt | sed 's/ //g'`
Then the $FILE will contain only concatenated string of $1 and $6 without spaces
 
Old 09-30-2012, 08:38 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
To start with, please use ***[code][/code] tags*** around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Code:
FILE='awk -F: '{print $1, $6} /tmp/file.txt'
The use of single quotes means you are storing the command string itself in the variable. I doubt hightly that that's what you want (and if it is, then you're still doing it wrong).

(Note too that the quoting above is wrong in any case, as there's an unmatched third one. I've fixed this in subsequent examples.)

Assuming you want to capture the output of awk into the variable, then you need to be using command substitution.

There are two ways to do it. The first is with backticks:

Code:
FILE=`awk -F: '{print $1, $6}' /tmp/file.txt`
However, for various reasons, including people confusing them with single quotes, they are deprecated. The preferred form is now $(..).

Code:
FILE=$( awk -F: '{print $1, $6}' /tmp/file.txt )

Once the content is in the variable (and assuming it's properly formed, as per druuna's post), you have to use it correctly. And that means quoting it correctly.

Short form: QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS.

You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (and any globbing patterns in them expanded as well). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Code:
mv "$FILE" /ora
Hopefully this will be enough to let you figure it out.


As a final point, since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them.

Last edited by David the H.; 09-30-2012 at 08:41 AM. Reason: quote fix
 
Old 09-30-2012, 02:00 PM   #9
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Rep: Reputation: Disabled
Perl Script for Trimming.

I have this perl script that does trimming.
Code:
#!/usr/bin/perl

while(<STDIN>)
{
my $line = $_;
chomp($line);

# Declare the subroutines
sub trim($);
sub ltrim($);
sub rtrim($);

# Create a test string
my $string = "  \t  Hello world!   ";
print trim($string)."\n";
print ltrim($string)."\n";
print rtrim($string)."\n";
}
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
# Left trim function to remove leading whitespace
sub ltrim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	return $string;
}
# Right trim function to remove trailing whitespace
sub rtrim($)
{
	my $string = shift;
	$string =~ s/\s+$//;
	return $string;
}
Hope this is 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
[SOLVED] How to change an environment variable sunnieside-up Linux - General 3 09-23-2011 07:39 PM
[SOLVED] Change PATH variable ataias Linux - General 5 03-12-2011 02:50 PM
Change PATH variable jimmydean Linux - Newbie 4 10-16-2010 06:24 PM
How to change the value in 'G_FILENAME_ENCODING' variable? simjii SUSE / openSUSE 1 02-04-2007 03:05 AM
I cannot change my PATH$ variable KptnKrill Linux - Newbie 6 08-17-2003 04:03 PM


All times are GMT -5. The time now is 06:44 PM.

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