Computing.Net > Forums > Unix > Pad an extra space in a variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Pad an extra space in a variable

Reply to Message Icon

Name: Joe Richardson
Date: January 16, 2003 at 07:21:01 Pacific
OS: HP-Unix
CPU/Ram: ??
Comment:

I thought this would be easy, but I can't figure it out....

Assume I have 3 variables with the following values:
C_MONTH=Jan
C_DAY=5
C_YEAR=2003

What I want to do is put these three variables into 1 variable like this:
C_DATE="$C_MONTH $C_DAY $C_YEAR"

The problem is that I want there to be two spaces between $C_MONTH and $C_DAY. In other words, if I echo $C_DATE, I want it to return this:
Jan 5 2003
I don't want it to return this:
Jan 5 2003

Below is some code (with comments) that I though would work, but doesn't.
----
#!/bin/ksh

C_MONTH=Jan
C_DAY=5
C_YEAR=2003

C_DATE="$C_MONTH $C_DAY $C_YEAR"

# The next line echos what I want the value of C_DATE to have.
echo $C_DATE | awk '{ print $1, " "$2, $3 }'

# How do I get C_DATE to equal what was echoed above?
# The following line doesn't work!
C_DATE=`echo $C_DATE | awk '{ print $1, " "$2, $3 }'`

# The next line should return "Jan 5 2003" NOT "Jan 5 2003".
echo $C_DATE
----



Sponsored Link
Ads by Google

Response Number 1
Name: anukta_c
Date: January 16, 2003 at 08:23:43 Pacific
Reply:

Try:
echo $C_DATE|awk '{printf("%s--%3s--%4s\n",$1,$2,$3)}'

I have marked the spaces as - , for readability.

-Anukta


0

Response Number 2
Name: James Boothe
Date: January 16, 2003 at 09:23:49 Pacific
Reply:

When you have multiple spaces in succession, you can protect those by enclosing in quotes. You did this at assignment time, but not when you did the echo. Although this web page will eat the extra space, the following assignment has two spaces preceding the 5:

C_DATE="Jan 5 2003"

The following command confirms the length of the assigned string:

expr length "$C_DATE"
11

The echo command, by default, will process invididual words within $C_DATE and separate with a single space, but again, you can protect those multi-spaces with quotes:

echo "$C_DATE"


0

Response Number 3
Name: Joe Richardson
Date: January 16, 2003 at 09:39:03 Pacific
Reply:

THANKS!

That was my problem, I was not enclosing my variable $C_DATE in quotes!



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


print only listing of fil... Unix scripting problem



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Pad an extra space in a variable

Find duplicate words in a file www.computing.net/answers/unix/find-duplicate-words-in-a-file/7999.html

using a pipe in a unix scrip www.computing.net/answers/unix/using-a-pipe-in-a-unix-scrip/4154.html

file size & name ->a variable ? www.computing.net/answers/unix/file-size-amp-name-a-variable-/3450.html