The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Appending data into a variable michaeltravisuk Shell Programming and Scripting 5 01-31-2009 04:20 AM
Appending to a variable? paqman Shell Programming and Scripting 0 08-14-2007 01:16 PM
appending spaces to a variable rallapalli Shell Programming and Scripting 2 08-13-2007 02:23 AM
Sed - Appending a line with a variable on it eltinator Shell Programming and Scripting 4 07-30-2007 10:15 AM
appending space in binary data in c arunkumar_mca High Level Programming 2 02-12-2007 09:30 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-04-2008
viv1 viv1 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 21
appending space to variable

Hi
I need to write a script where there the user enters 3 input parameter
variable
number
the program should ask the user left or right
if it is left , the number specified that many spaces should be added to the value in front of the value and saved in the samee variable itself and if it is right that many number of spaces should be added in the right side

for ex
variable = vivek
number =4
and when user echo $variable output should be
if user opts for right then output = (4 spaces in front)vivek
if user opts for left then output =vivek(4 spaces in back)
  #2 (permalink)  
Old 02-04-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,957
sample snippet in perl

Code:
$indendSpace += 2;
$spaceVar = " " x $indendSpace;

$var =~ s/^/$spaceVar #at the start
$var =~ s/$/$spaceVar #at the end
  #3 (permalink)  
Old 02-04-2008
viv1 viv1 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 21
I need it in korn shell

need it in korn shell
  #4 (permalink)  
Old 02-04-2008
viv1 viv1 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 21
Thanks but need it in korn shell
  #5 (permalink)  
Old 02-04-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,934
Code snippets in ksh-93

Code:
$pad="    "
$var="vivek"
$ newvar=$pad$var
$ echo $newvar
vivek
$ echo ${#newvar}
9
$ echo "$newvar"
    vivek
$ L=9
$ new=$(printf "%${L}s" "$var" )
$ echo "$new"
    vivek
$
  #6 (permalink)  
Old 02-04-2008
V3l0 V3l0 is offline
Registered User
  
 

Join Date: Nov 2007
Location: Belgium & France
Posts: 70
Code:
#!/bin/ksh

echo "Input variable: \c" ; read var
echo "Input number: \c" ; read pad
echo "Input Left or Right padding (L or R): \c" ; read lr

lr=$(echo $lr | tr '[a-z]' '[A-Z]')

if [ "$lr" != "R" ] && [ "$lr" != "L" ]
then
        echo Invalid padding $lr !
        exit
fi

while [ $pad != 0 ]
do
        if [ "$lr" = "R" ]
        then
                var=$var" "
                let pad=$pad-1
        else
                var=" "$var
                let pad=$pad-1
        fi
done

echo "[$var]"
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0