Want to add trailing spaces to the variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Want to add trailing spaces to the variable
# 1  
Old 07-09-2012
Want to add trailing spaces to the variable

I want to keep string/varible length to 10 even its actual length is less than 10(may be no value). so, i want to add trailing spaces to my string. Smilie
"typeset -L10 myvarible" is not working, its saying invalid typset -L option.
Can you please advise.
# 2  
Old 07-09-2012
Code:
myvar="123"
myvar=$(printf "%-10s" $myvar)
echo ":$myvar:"

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 07-10-2012
Jim, Thank you. that worked like a charm.Smilie

---------- Post updated 07-10-12 at 09:52 AM ---------- Previous update was 07-09-12 at 09:36 PM ----------

Jim, i got into another issue when iam using above code.
it is behaving weared when i had space in $myvar value.

Code:
myvar="12 123"
myvar=$(printf "%-10s" $myvar)
echo ":$myvar:"

result is coming -> "
Code:
:12 123 :

"

but i want -> "
Code:
:12 123:

" can you please help me out.
Thanks.

---------- Post updated at 11:47 AM ---------- Previous update was at 09:52 AM ----------

Jim, i got into another issue when iam using above code.
it is behaving weared when i had space in $myvar value.

Code:
myvar="12 123"
myvar=$(printf "%-10s" $myvar)
echo ":$myvar:"

result is coming -> "
Code:
:12(8 spaces)123(7 spaces):

"

but i want -> "
Code:
:12 123(4spaces):

" can you please help me out.
Thanks.

Last edited by Scrutinizer; 07-10-2012 at 04:15 PM.. Reason: code tags
# 4  
Old 07-10-2012
printf is padding two arguments (neither of which contains any spaces) because the shell is splitting the unquoted expansion of $myvar. Double quote your variable.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 07-10-2012
That worked. Thank you. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove leading and trailing spaces for variable in shell script?

Hi I have variable named tablename. The value to tablename variable has leading and trailing white spaces. How to remove the leading and training white spaces and write the value of the tablename without space to a file using shell script. ( for e.g. tablename= yyy ) INPUT ... (10 Replies)
Discussion started by: pottic
10 Replies

2. UNIX for Beginners Questions & Answers

Trim trailing spaces from file

I have a file like this. hari,corporationbank,2234356,syndicate ravi,indian bank,4567900000000,indianbank,accese raju,statebank of hyderabad,565866666666666,pause Here each record has different record length and there are blank spaces... (8 Replies)
Discussion started by: kshari8888
8 Replies

3. Shell Programming and Scripting

How to remove trailing spaces from a variable?

I am getting a value from a csv file using CUT command, however the command extracting the records with trailing spaces. I am using the result into a sql session to fetch data, because of the trailing spaces the sql session is unable to fetch any data. Please let me know, how to remove this... (2 Replies)
Discussion started by: mady135
2 Replies

4. Shell Programming and Scripting

[solved] Trailing spaces

Hello People How to check whether lines in a text file have trailing spaces or not and if a line have trailing spaces then how many trailing spaces line have? Regards ARvind kumar (5 Replies)
Discussion started by: arvindk.monu
5 Replies

5. Shell Programming and Scripting

How to add trailing spaces to have file with lines of the same length?

I have textfile (source.txt) with different length of lines in it. Can anybody help to compose a script under bash which would add suitable number of trailing spaces to the end of each line so that after the processing the each line would have the same (let's say 100 char) length? Output can be... (6 Replies)
Discussion started by: sameucho
6 Replies

6. Shell Programming and Scripting

Remove trailing spaces from file

I'm currently writing my sql results to a file and they have trailing spaces after each field. I want to get rid of these spaces and I'm using this code: TVXTEMP=$(echo $TVXTEMP|sed -e 's/\ //g') It doesn't work though. I'm not familiar with sedscript, and the other codes I've found online... (6 Replies)
Discussion started by: avillanueva
6 Replies

7. UNIX for Dummies Questions & Answers

Remove Trailing spaces after a delimiter

Hi, I am trying to remove trailing white spaces using this command in awk nawk -F '|' '/^TR/{t = $4 }/^LN/{gsub(/ */,"");printf "%s|%s\n", t, $0 }' $i>>catman_852_files.txt My delimiter is '|'. THere are some description fields which are being truncated. I dont want to remove spaces... (1 Reply)
Discussion started by: kiran_418
1 Replies

8. UNIX for Dummies Questions & Answers

How to remove trailing spaces

Hi, I have a file like this (ADD_MONTHS((Substr(Trim(BOTH FROM Translate(Maximum(closeDa ------------------------------------------------------------ 2007-06-30 00:00:00 I have a requirement where i need just the date. When i do: tail -1... (2 Replies)
Discussion started by: mahek_bedi
2 Replies

9. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

10. Shell Programming and Scripting

Leading and Trailing Spaces

Hi, how to i remove leading and trailing spaces from a line? the spaces can be behind or in front of any field or line example of a line in the input data: Amy Reds , 100 , /bin/sh how to i get it to be: Amy Read,100,/bin/sh i saw something on this on the Man pages for AWK... (7 Replies)
Discussion started by: sleepster
7 Replies
Login or Register to Ask a Question