Padding With White Space Between Variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Padding With White Space Between Variables
# 1  
Old 02-26-2009
Padding With White Space Between Variables

Dear Users,

How do we pad with white space of particular length between two variables.

For Example:

Suppose i define the variables as follows:

a='toyota'
b='camry'
c='honda'
d='accord'
e=`echo "$a"'\n'"$b"`
f=`echo "$c"'\n'"$d"`

If i do an echo on variables e and f i get :

echo "$e" will give

toyota
camry

echo "$f" will give

honda
accord

I want to pad white space of length 30 between variables e and f so that i get the result as :

honda toyota
accord camry

I tried using the below but it is not working:

typeset -R20 e

if i give echo "$f""$e" it is giving the result as :

honda
accord toyota
camry

which is wrong. I want the final result as:

honda toyota
accord camry

honda accord and toyota camry should be separated by white space of length 30.


How can i do this in unix?

Thanks
Sandeep
# 2  
Old 02-26-2009
Here is something i have written once. It does a bit more than what you asked for as it can pad strings with arbitrary characters and do it left-aligned, right-aligned or centered. The f_CheckInteger() function used simply checks if a parameter is an integer or not, you could skip that test.

I hope this helps.

bakunin

Code:
# f_PadStr                                         pad a string with fill chars
# ------------------------------------------------------------------------------
# Author.....: Wolf Machowitsch
# last update: 2001 05 30    by: Wolf Machowitsch
# ------------------------------------------------------------------------------
# Revision Log:
# - 0.99   2000 11 10   Original Creation
#                       -
#
# - 1.00   2001 01 12   Production Release
#                       did the docu, as ever
#
# - 1.01   2001 05 30   bugfix release
#                       finally did the centered padding feature
#
# ------------------------------------------------------------------------------
# Usage:
#
#     f_PadStr char* String int Length [char Fillchar [int Mode] ]
#
#     Example:
#     #!/bin/ksh
#     # example script for using f_PadStr()
#     typeset Mystring="abc"
#     # fill the variable Mystring to length 6 with leading 'x'
#     f_PadStr Mystring 6 x 1
#     print - $Mystring          # yields 'xxxabc'
#     typeset Mystring="ab"
#     # fill the variable Mystring to length 6 with trailing 'x'
#     f_PadStr Mystring 6 x 2
#     print - $Mystring          # yields 'abxxxx'
#     exit
#
# Prerequisites:
#
#     none
#
# Functional dependecies:
#
#     f_CheckInteger()
#
# ------------------------------------------------------------------------------
# Documentation:
#     f_PadStr() pads strings with a specific fill character to a given
#     given length. The string has to be passed BY REFERENCE in $1, that
#     is, not the strings value is passed but the name of variable containing
#     it. The desired length has to passed in $2 as integer value, the mode
#     describes how the string is to filled up to the specified length. Mode 1
#     (the default) means with leading fillchars, mode 2 means with trailing
#     ones. If the string is longer than the specified length an error (2)
#     is returned and the string left unmodified.
#
#     f_PadStr char* String int Length [char Fillchar [int Mode] ]
#     Parameters: char* String    the name of the variable holding the string
#                 int   length    the desired length of the result string
#                 char  fillchar  single character used to fill the string
#                 int   Mode      1: fill with leading fillchars
#                                 2: fill with trailing fillchars
#                                 3: center string in fillchars
#
#     returns:    0 success
#                 1 parameter error
#                 2 original string longer than desired length
# ------------------------------------------------------------------------------
# known bugs:
# -   while spaces *in* the string are handled ok, leading and trailing
#     whitespace are cut off at the start of the function
#
# ------------------------------------------------------------------------------
# .....................(C) 2000 Wolf Machowitsch ...............................
# ------------------------------------------------------------------------------

f_PadStr ()
{

typeset -i iRetVal=0                             # return value
typeset    chWork="$(eval print - "\${$1}")"     # work buffer
typeset    chTmp=""                              # temp buffer
typeset -i iLength=0                             # length of result string
typeset    chFill="$3"                           # fillchar
typeset -i iStrLen=0                             # length of input string
typeset -i iMode=0                               # mode (leading/trailing)
typeset -i lLeftRight=0                          # toggle for centered padding


                                                 # check parameter integrity
if [ $(f_CheckInteger "$2" 2 ; print $?) -eq 0 ] ; then
     iLength=$2                                  # result length >= 2 ?
else
     iRetVal=1
fi

if [ -n "$4" ] ; then
     if [ $(f_CheckInteger "$4" 1 3 ; print $?) -eq 0 ] ; then
          iMode=$4                               # Mode=1,2 ?
     else
          iRetVal=1
     fi
fi

if [ -n "$(print - "$chFill" | sed 's/^.//')" ] ; then
     iRetVal=1                                   # fillchar is single char ?
fi

if [ iRetVal -eq 0 ] ; then                      # get length of input string
     chTmp="$chWork"
     (( iStrLen=0 ))
     while [ -n "$chTmp" ] ; do
          chTmp=$(print - "$chTmp" | sed 's/^.//')
          (( iStrLen += 1 ))
     done

     if [ $iStrLen -gt $iLength ] ; then
          iRetVal=2
     fi
fi

if [ $iRetVal -eq 0 ] ; then                      # pad string
     while [ "$iStrLen" -lt "$iLength" ] ; do
          if     [ $iMode -eq 1 ] ; then
               chWork="$chFill$chWork"            # leading fillchar
          elif [ $iMode -eq 2 ] ; then
               chWork="$chWork$chFill"            # trailing fillchar
          elif [ $iMode -eq 3 ] ; then
               if [ $lLeftRight -eq 0 ] ; then
                    chWork="$chWork$chFill"       # padding centered
                    lLeftRight=1
               else
                    chWork="$chFill$chWork"
                    lLeftRight=0
               fi
          fi
          (( iStrLen += 1 ))
     done
     eval $1=\"$chWork\"                          # copy result back to $1
fi

return $iRetVal
}
# --- EOF f_PadStr

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add white space

hi guys how can i add spacein file name with sed if strings have no space around dash input 19-20 ( 18-19 ) ABC-EFG output after add white space 19 - 20 (18 - 19 ) ABC - EFG thx in advance (2 Replies)
Discussion started by: mhs
2 Replies

2. Shell Programming and Scripting

Padding space with each column

Hi How do I a add space with each column . Each column has fixed size like 1 st column = 10 2nd column = 20 In each column data can be of variable length.So rest of the length should be space Frank Student Sue Admin (3 Replies)
Discussion started by: Chinky23
3 Replies

3. UNIX for Dummies Questions & Answers

filename with white space

our user creates a text file with a white space on the filename. this same file is transfered to unix via automation tool. i have a korn shell script that reads these files on a input directory and connects to oracle database to run the oracle procedures which will load the data from each of the... (2 Replies)
Discussion started by: wtolentino
2 Replies

4. Shell Programming and Scripting

sed + white space

Hi, What sed command (if sed is the right command) can remove ALL white space from my file. I have a csv, except I want to remove all white space between commas and characters. My idea (without testing) sed 's/ //g' Is there a better way? (18 Replies)
Discussion started by: mcclunyboy
18 Replies

5. UNIX for Dummies Questions & Answers

SED with White Space

Dear Members, Suppose i have a variable test which stores a string as below: test='John drives+++++++++a+++++car' now i want to use sed on the above variable and replace + with a white space, so that i get echo $test should give me 'john drives a car' Between... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

6. Shell Programming and Scripting

how to protect white space in for loop

Hi All, I know there's a really simple answer to this but I just can't think of it :) I'm processing a file which has lines containing white space i.e. And I want to perform some awk on each line but when I do the following: for US in $( cat /tmp/unique-strings.tmp | sed 's/\/\\]/g'... (6 Replies)
Discussion started by: pondlife
6 Replies

7. UNIX for Dummies Questions & Answers

Quick help on space padding

Hi all, I need to create a header record of width 121 charecter with below fields, position 001-001 - 0 position 002-15 - filled with space position 016-020 - date value position 021-121 - filled with space Iam using the below code: echo 0"14 spaces"$date"100 spaces" >> output.txt ... (3 Replies)
Discussion started by: Lokesha
3 Replies

8. Shell Programming and Scripting

stripping white space...

Hi All; Having a problem with a file.. the file contains the following data... (a snapshot) 1331F9E9DB7C2BB80EAEDE3A8F043B94,AL7 1DZ,M,50 186FDF93E1303DBA217279EC3671EA91,NG5 1JU,M,24 3783FFAF602015056A8CD21104B1AAAF,CH42 4NQ,M,17 It has 3 columns sepreated by a , the second column... (7 Replies)
Discussion started by: Zak
7 Replies

9. Shell Programming and Scripting

How to get just the word and clean the white space?

Hi, I need to check if the value returned by this query is bigger then 20000. It's not working! I think that the problem is that the return is with white spaces. How to solve this? Tks, Paulo Portugal. ####################### RESPOSTA=`/oracle/app/product/10.2/bin/sqlplus -s <<EOF / as... (2 Replies)
Discussion started by: paulofp
2 Replies

10. Programming

Padding variables

Is there a function in c that will allow me to pad variables? I have an int that can't be longer than 10. I need to pad a numeric value with leading zeros 314 0000000314 (1 Reply)
Discussion started by: flounder
1 Replies
Login or Register to Ask a Question