How to insert a space and assign the value to another variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert a space and assign the value to another variable?
# 1  
Old 11-03-2014
How to insert a space and assign the value to another variable?

Hello,

I need to insert a space between 2 strings. I used many techniques and all of them worked but when I assign the value to another variable then the inserted space vanishes, strange! Please advise.

Code:
# dat=`date |awk '{print $2,$3}'`
# echo $dat
Nov 3

The above is perfectly fine. Now

Code:
# echo ${dat}|sed "s/ /  /g"
Nov  3
#

The above also is perfectly fine. Now if I assign the same output to another variable the extra space vanished.

Code:
# dat2=`echo ${dat}|sed "s/ /  /g"`
# echo $dat2
Nov 3
#

Please advise how to retain the extra space in the new variable. Sorry if its a basic question but I never noticed such things.

TIA
# 2  
Old 11-03-2014
The shell is stripping your additional space. Quote the $dat2 string to protect it from the shell parameter expansion.

Code:
# dat2=`echo "${dat}"|sed "s/ /  /g"`
# echo "$dat2"
Nov  3

You can see this issue even without variables:

Code:
# echo one      two     three
one two three

# echo "one      two     three"
one      two     three

# 3  
Old 11-03-2014
It's there. Try double quotes: echo "$dat2"
# 4  
Old 11-03-2014
Quote:
Originally Posted by Chubler_XL
The shell is stripping your additional space. Quote the $dat2 string to protect it from the shell parameter expansion.

Code:
# dat2=`echo "${dat}"|sed "s/ /  /g"`
# echo "$dat2"
Nov  3

You can see this issue even without variables:

Code:
# echo one      two     three
one two three

# echo "one      two     three"
one      two     three

I cant pm you as i dont have enough post counts, i think i found a way to do my work not sure but worth a try. ill post in short time my work :/
# 5  
Old 11-03-2014
You are not supposed to PM technical questions in any case.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

3. Shell Programming and Scripting

how to insert space in alphanumeric string

Hi everyone, I want help to insert space between digits and letters in a alphanumeric string. INPUT TRY234TER PHY1TYR EXPECTED OUTPUT TRY 234 TER PHY 1 TYR The lines always begin with the letters and the alphabets will be a three letter combination before and after the number. The... (2 Replies)
Discussion started by: kaav06
2 Replies

4. Shell Programming and Scripting

Insert text space

I have the following texts in a .txt file, 22/02 23/02 24/02 25/02 26/02 Bike speed (kmph) 004 004 004 004 004 22/02 23/02 24/02 25/02 26/02 Bike speed (kmph) 004 007 007 007 011 I am trying to export it to .csv by doing cat nic.txt > nic.csv 22/02 23/02 are date and this has to... (7 Replies)
Discussion started by: nicolethomson
7 Replies

5. Shell Programming and Scripting

Looking for help with script to assign all disk space to slice#0 on multiple disks of varying sizes

Hi Folks, I am trying to make a script to assign all diskspace to slice 0, on multiple sized disks. Since the disks are new they may need to be labelled also to avoid the error: Cannot get disk geometry Below is my code struggling with logic which doesn't seem to be producing the desired... (0 Replies)
Discussion started by: momin
0 Replies

6. Shell Programming and Scripting

Shell assign variable to another variable

How can I assign a variable to an variable. IE $car=honda One way I can do it is export $car=honda or let $car=2323 Is there any other ways to preform this task (3 Replies)
Discussion started by: 3junior
3 Replies

7. Programming

Assign variable for INSERT INTO statement

Hello, Can anyone tell me that, How can I assign variable to shell script variable, which i need to use in INSERT INTO statement? my shell script variables are, EMPNAME=`regular expression` EMPID=`regular expression` EMPBDATE=`regular expression` Now through ksh script I am... (16 Replies)
Discussion started by: Poonamol
16 Replies

8. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

9. Shell Programming and Scripting

Insert space between two words

Hi, I need to insert space between words on my output in UNIX other than the single space given by the space bar on my keyboard, e.g when are you going. (There should be 4 spaces between each of these words) rather than when are you going Can anyone help me with... (3 Replies)
Discussion started by: divroro12
3 Replies

10. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies
Login or Register to Ask a Question