Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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=2003What 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 2003Below is some code (with comments) that I though would work, but doesn't.
----
#!/bin/kshC_MONTH=Jan
C_DAY=5
C_YEAR=2003C_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
----

Try:
echo $C_DATE|awk '{printf("%s--%3s--%4s\n",$1,$2,$3)}'I have marked the spaces as - , for readability.
-Anukta

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"
11The 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"

![]() |
print only listing of fil...
|
Unix scripting problem
|

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