Append zeros before a value as per variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append zeros before a value as per variable
# 1  
Old 10-09-2009
Question Append zeros before a value as per variable

Hello-

I have a variable which contains a number, I need to populate number of zeros before another value as per this variable value.

for example:

I have variable X whose content is 5, variable Y whose content is 123

Now append number of zeros as per variable X before varible 'Y' and the hole resultant has to be captured in another variable.

expected result : 00000123

Thanks
Raghavendra
# 2  
Old 10-09-2009
Hi,

Is this what you are after?

Code:
$ X=5
$ Y=123
$ printf "%0*d" $(( $X + ${#Y} )) $Y
00000123

# 3  
Old 10-09-2009
Another way, less shell specifuc:

Code:
#  x=5;y=123;t=$(echo  | nawk -v X=x -v Y=$y '{printf "%05s%d",$1,Y}'); echo $t
00000123

HTH
# 4  
Old 10-09-2009
Quote:
Originally Posted by Tytalus
Another way, less shell specifuc
My shell solution should be working in bash, ksh, zsh, dash and probably other shells as well.

Last edited by ripat; 10-09-2009 at 08:37 AM.. Reason: No, it's not working in csh.
# 5  
Old 10-11-2009
Quote:
Originally Posted by pasupuleti81
...
I have variable X whose content is 5, variable Y whose content is 123
Now append number of zeros as per variable X before varible 'Y' and the hole resultant has to be captured in another variable.
expected result : 00000123
...
One way to do it with Perl:

Code:
$
$ x=5
$ y=123
$
$ ##
$ perl -le "print '0'x$x,$y"
00000123
$
$ # variable assignment
$ z=$(perl -e "print '0'x$x,$y")
$ echo $z
00000123
$


tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - proper way to append variable to stderr

Hello, Can you please if the bellow is the proper way of appending a variable to the stderr: The easiest way to test this,I was able to imagine, was by touching 5 files and afterwards looping trough to the results: -rw-r--r-- 1 ab owner 0 Sep 14 13:45 file1 -rw-r--r-- 1 ab owner 0 Sep... (7 Replies)
Discussion started by: alex2005
7 Replies

2. Shell Programming and Scripting

Append variable value with sed

I have a requirement where I need to add a variable value based on a pattern match. When I try this it works: sed '/IS_ETL_DEV/a\ ABCD' File1.txt > File1.tmp Now I want to replace IS _ETL_DEV with a variable like $Pattern and replace the values ABCD with a variable $Var the value "$$XYZ=1".... (4 Replies)
Discussion started by: vskr72
4 Replies

3. Shell Programming and Scripting

Append 0 to a variable

Hi, I have a variable... it can take a value from 1 to 99... now when it is 1 to 9 i want to save it as 01 to 09 in the same variable... can this be done using sed or awk??? a=1 i want it as a=01 Thanks in advance... (8 Replies)
Discussion started by: Nithz
8 Replies

4. Shell Programming and Scripting

append to same string variable in loop

I want to append values to same string variable inside a recursive function that I have .. I do not want to write to any file but use a variable.. Can anyone please help with it? Thanks in advance. (6 Replies)
Discussion started by: Prev
6 Replies

5. UNIX for Dummies Questions & Answers

how do I append new lines to awk variable?

I want to build an array using awk, consisting only of a subset of lines of a file. I know how to have awk assess whether a key phrase is in a particular line, but I can't find anywhere how to then append the line containing that phrase to an array that has previously-found lines also containing... (9 Replies)
Discussion started by: pts2
9 Replies

6. Shell Programming and Scripting

Add leading zeros in floating point variable

I need to add leading zeros in a floating point numbers. The length of the number should be 13 including decimal. The input number is changing so number of leading zeros is not fix. For example input output 216.000 000000216.000 1345.000 000001345.000 22345.500 ... (4 Replies)
Discussion started by: reeta_shri
4 Replies

7. Shell Programming and Scripting

Append variable to only certain lines

Hi There! I'm trying to write a shell script which generates a random hexadecimal number and then appends it to the end of lines XX onwards of a certain file. XX is determined by a certain string on the previous line. Thus for example, if the input file is I search for the string... (3 Replies)
Discussion started by: orno
3 Replies

8. Shell Programming and Scripting

Extract Variable and Append it to the file

I have a file named xyz_extract_file_20091201.dat and I need to strip out 20091201 from the file name and append this value as a first field in all the records in the file. Can you please help? Thanks. Eg. Input File: xyz_extract_file_20091201.dat ------------ Campaign1,A2347,Grp_id1... (1 Reply)
Discussion started by: shekharaj
1 Replies

9. Shell Programming and Scripting

How to delete trailing zeros from a variable

Hi All I want to delete trailing zeros from varible. ex: if variable value is 1234.567000 result as 1234.567 if variable has 1234.0000 result as 1234 if variable as abcd.fgh result as abcd.fgh Can somone give me a solution using awk? (16 Replies)
Discussion started by: Chandu2u
16 Replies

10. Shell Programming and Scripting

Removing leading zeros from a variable

How do I remove or add leading zeroa from a variable. To make variable 10 characters long when adding zeros. (6 Replies)
Discussion started by: toshidas2000
6 Replies
Login or Register to Ask a Question