Computing.Net > Forums > Unix > concat strings and keep spaces

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.

concat strings and keep spaces

Reply to Message Icon

Name: kuhjaay
Date: August 19, 2004 at 13:25:10 Pacific
OS: HP-UX
CPU/Ram: ?
Comment:

I'm a newbie to unix scripting and I'm trying to find the most efficient way to concatenate two string variables together but to retain the spaces for output to a fixed width file. (I'm using KSH on a Unix platform)
ex:
var1="99999 " (5 blanks, left justified)
var2="01012004"

How can I concat these two vars together and store the value into a var named 'string', w/o losing my spaces? I tried using awk, but maybe my syntax is wrong:

string=$(awk '{printf("%-10s%-8s\n",$var1,$var2);}')
echo "$string"

I keep getting this error:
awk: Field $() is not correct.



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: August 19, 2004 at 14:50:07 Pacific
Reply:

It is just a simple concatenation (string=$var1$var2). But when you echo $string, enclose in double-quotes - otherwise, echo will echo each word separated by a single space.

var1="99999     "  # (5 trailing spaces)
var2="01012004"
string=$var1$var2
echo $string
99999 01012004
echo "$string"
99999     01012004

And awk cannot see environment variables by default, although there are several ways for awk to get at them.  Below, I just pipe them in:

string=$(echo $var1 $var2 | awk '{printf("%-10s%-8s\n",$1,
$2);}')
echo "$string"
99999     01012004


0

Response Number 2
Name: kuhjaay
Date: August 23, 2004 at 07:36:57 Pacific
Reply:

Thanks for explaining that bit about awk and environment variables. Both of your examples did the trick.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Help with Comparing multiple files ...



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: concat strings and keep spaces

Search a string and generate a error message. www.computing.net/answers/unix/search-a-string-and-generate-a-error-message/8386.html

check for a palindrome www.computing.net/answers/unix/check-for-a-palindrome/3095.html

read command with trailing spaces www.computing.net/answers/unix/read-command-with-trailing-spaces/7396.html