LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2008, 03:18 PM   #1
Juance
LQ Newbie
 
Registered: Jun 2008
Posts: 2

Rep: Reputation: 0
Thumbs up How can I conserve the blanks when I assign to env variable?


Hi All,

I'm working on Compaq Computer Corporation Tru64 UNIX V5.1.

see I need store the result of this command in a env variable:
(please where you see "-" is blanks)

$ sed -n 3p file
0000311CDS-009384---------------0.00------------0.00-----------0.00

this is my result:
$ line=`sed -n 3p file`
$ echo $line
0000311CDS 009384 0.00 0.00 0.00

I need conserve the blanks! but these are removed.

IMPORTANT NOTE!: I found the same frustation making this thread!
if i write consecutive blanks these are automatically removed after post, thats is why I replaced the " " by "-".

Could you please help me??
Thanks in advace!!!
 
Old 06-13-2008, 04:01 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 76
Ignore what I said, the problem was in the quoting, not the field separator.

Last edited by osor; 06-13-2008 at 04:36 PM. Reason: Oops.
 
Old 06-13-2008, 04:07 PM   #3
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
It is probably only removing the blanks when you print (echo), they should still be in the variable. Quoting it should solve the problem.

Code:
echo "$line"
Actually, it's not a bad idea to quote all variables in bash, including assignments, unless you explicitly want to word split (automatic word splitting is stupid behaviour IMO).
 
Old 06-13-2008, 04:11 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Two things are at play here. First, if you post your formated text in [CODE] tags, the formatting will be preserved.
Second, the spaces are kept intact when assigned to your variable. If, however, you pass the variable as an argument to a function or as a commandline argument, the shell will expand it into multiple whitespace-delimited arguments. If you want the variable to be used as a single argument, then enclose it in double quotes:
Code:
grep "$line" file
--- rod.
 
Old 06-17-2008, 09:28 AM   #5
Juance
LQ Newbie
 
Registered: Jun 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks guys!!!
 
  


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
socks proxy - env variable andrewlkho Linux - Networking 1 06-23-2013 10:58 PM
Help needed in setting env variable mageshvan Programming 2 10-01-2007 02:47 AM
env variable for proxy: syntax FrayAdjacent Linux - Networking 10 07-13-2005 06:44 PM
env variable allelopath Linux - Software 3 04-14-2005 01:07 PM
Changing $PATH env variable alzogbi Linux - Newbie 2 03-26-2004 03:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:59 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