Visit Our UNIX and Linux User Community

Left filled with zeros

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Left filled with zeros
# 1  
Old 02-25-2008
Question Left filled with zeros

A question about numeric display there...
Is there any option in awk that would prints a number with "left filled with zero" format, and how does it work with variable length definition?

For instance IŽd like my variable to be left filled with 0s for a length of 3 in total:

4 would be 004
12 would be 012
312 would be 312

Any help would be much appreciated, cheers.
# 2  
Old 02-25-2008
echo "4" | awk '{printf("%03d\n",$0)}'

this may help you
# 3  
Old 02-25-2008
Here is an example of one way to do it:



cat <<EOT >$tmp

awk '{ printf "%03d\n", $1 }' $tmp

rm $tmp
exit 0

# 4  
Old 02-25-2008
Many thanks

Previous Thread | Next Thread
Test Your Knowledge in Computers #383
Difficulty: Medium
Solaris 2.5.1 was registered as UNIX 95 compliant on the PReP PowerPC platform.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing parameter filled variable to a script.

Say I have something like this to try and build a parameter string: OPT1="This is my Option"; OPT2="/folder/file"; PARMS="-u me -J \"$OPT1\" -g \"$OPT2\""; ./script.ksh -n 1 $PARMS; ./script.ksh -n 2 $PARMS; ./script.ksh -n 3 $PARMS; ./script.ksh -n 4 $PARMS; But this doesn't work. ... (2 Replies)
Discussion started by: Devyn
2 Replies

2. Shell Programming and Scripting

Pad Zeros at the end

I have number/strings like below input =23412133 output = 234121330000 (depends on the number give at runtime) i need to padd zeros based on runtime input . i tried below printf ' %d%04d\n', "23412133"; But the precision 4 is static here how can i pass this as runtime input. i am... (11 Replies)
Discussion started by: greenworld123
11 Replies

3. Solaris

Solaris full /tmp - du and df different swap NOT filled

Hello all, The issue is # df -h /tmp Filesystem size used avail capacity Mounted on swap 4.0G 4.0G 8.7M 100% /tmp # du -sh /tmp/ 87M /tmp By now you probably will say that this is open file destriptor issue. Well no, nothing... (2 Replies)
Discussion started by: click
2 Replies

4. AIX

Check that memory page is filled by zeros when a process gets it in first time

I have to check whether AIX fills physical memory pages by zeros when they are given for a new process (or may be when they are freed from an address space, but it's hardly probable). In other words when a process gets a new memory page, this one must be cleaned. I've solved this issue for... (2 Replies)
Discussion started by: sokolovm
2 Replies

5. Shell Programming and Scripting

Padding with zeros.

Hi Friends, I would like to left pad with "0's" on first column say (width six) I have a large file with the format: FILE: 1: ALFRED 84378 NY 8385: JAMES 88385 FL 323: SMITH 00850 TX My output needs to be like: 000001: ALFRED 84378 NY 008385: JAMES 88385 FL 000323: SMITH... (10 Replies)
Discussion started by: sbasetty
10 Replies

6. Shell Programming and Scripting

read space filled file and replace text at specific position

Hi I have a spaced filled file having records like below: What I want is to read line having RT3 at position 17-19 then go to position 2651 check the 18 characters (might be space filled till 18 characters). This position should have a... (6 Replies)
Discussion started by: COD
6 Replies

7. UNIX for Advanced & Expert Users

trimming zeros

Hi, I want to trim +with leading zero's with amount fields.I know using awk for trimming leading zeros with +,but I want get the entire row itself. cat file_name |awk -F " " '{printf "%14.4f%f\n",$4}' ex: 10 xyz bc +00000234.4500 20 yzx foxic +002456.000 Expexted 10 xyz bc... (3 Replies)
Discussion started by: mohan705
3 Replies

8. UNIX for Dummies Questions & Answers

pad Zeros

Hi can I know command to pad Zeros to a value I get 16 and I need to send 0000000016 (5 Replies)
Discussion started by: mgirinath
5 Replies

9. Shell Programming and Scripting

Leading zeros

How to insert leading zeros into a left-justisfied zip code? e.g. Zip code is written as 60320 which is left-justified to make it be read as 0060320. We have to move it to right-justifiable then insert 2 leading zeros into it... ;) (1 Reply)
Discussion started by: wtofu
1 Replies

10. UNIX for Advanced & Expert Users

'tar -xvf' command filled up /dev/root

On a newly configured hp unix server, I got the following error while I was trying to untar a file: tar -xvf 9201rdbms.tar .... /htc_ora_prod/stage9.2.0_64bit/Disk2/stage/Components/oracle.rdbms.seeddb.compoltp/, 174282115... (2 Replies)
Discussion started by: YuChing
2 Replies

Featured Tech Videos