zero padding problem (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting zero padding problem (bash)
# 1  
Old 01-09-2012
Java zero padding problem (bash)

Hi there,

I need to loop some values,

Code:
for i in $(seq $first $last)
do
  does something here
done

for $first and $last, i need it to be of fixed length 5. so if the input is 1, i need to add zeros in front such that it becomes 00001. It loops till 99999 for example, but the length has to be 5.

eg 00002, 00042, 00212, 012312 and so forth.

any idea on how i can do that? thanks!
# 2  
Old 01-09-2012
Code:
$> read first
432
$>printf "%05d\n" $first
00432

# 3  
Old 01-09-2012
Code:
for i in {00001..99999}
do
     does something here
done

or
Code:
for i in $(seq -w ${first} ${last})
do
     does something here
done

# 4  
Old 01-09-2012
Hi.

Similarly to c:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate c-like for-loop in bash.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

pe
first=17
last=21
for (( i=first ; i<=last ; i++ ))
do
  printf "%05i\n" $i
done

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39

00017
00018
00019
00020
00021

See man bash for details ... cheers, drl
# 5  
Old 01-09-2012
Another alternative,
Code:
for i in `seq -f%05g $first $last`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Padding a csv value with 0's

I have this csv file that I would like to sort on the 20th and 21st field. They are high lighted below. My challenge is that when I sort on those fields they are not in order as I would have liked. It seems like I have to pad those fields to the longest value in that fields data. ... (6 Replies)
Discussion started by: GroveTuckey
6 Replies

2. Shell Programming and Scripting

Sort, sed, and zero padding date column csv bash scripting

Hello people, I am having problem to sort, sed and zero padding of column in csv file. 7th column only. Input of csv file: 1,2,3,4,5,6,4/1/2010 12:00 AM,8 1,2,3,4,5,6,3/11/2010 9:39 AM,8 1,2,3,4,5,6,5/12/2011 3:43 PM,8 1,2,3,4,5,6,12/20/2009 7:23 PM,8 Output:... (5 Replies)
Discussion started by: sean1357
5 Replies

3. UNIX for Dummies Questions & Answers

Is there a way to add padding to a bash shell script?

I'm grepping several lines and I want all of them to display with the same padding, is there a command to add whitespace before a line? (1 Reply)
Discussion started by: jcnewton13
1 Replies

4. Shell Programming and Scripting

bash padding

Hi all Is there a way to pad the output of a bash script see that code below for i in `sed -n '/Start Printer/,/End Printer/p' /u/ab/scripts/hosts.conf | awk '!/^#/ {print $2}' | egrep -v 'broke|primera' `; do pages=`snmpget -Ov -v1 -c public $i sysLocation.0 | awk '{print $2}'` ... (3 Replies)
Discussion started by: ab52
3 Replies

5. Shell Programming and Scripting

Padding leading zero

hi All i am new to linux... source txt .. 281-BUM-5M BUM-5M 0 0 282-BUM-5M BUM-5M 0 0 83-BUM-5M BUM-5M 0 0 is it possible to use bash script to convert to (remove the "-" and fill up to 4 digit" ? 0281 BUM-5M BUM-5M 0 0 0282 BUM-5M BUM-5M 0 0 0083 BUM-5M BUM-5M 0 0 thanks a ... (5 Replies)
Discussion started by: samoptimus
5 Replies

6. 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

7. Shell Programming and Scripting

bash typeset padding with zeros

Hi everybody, I have a question about typesetting. I originally wrote a script for use with ksh and now I am on a system that I cannot modify, and it only has bash. In the original script I just did typeset -RZ4 variable and it would add the leading zeros. In bash, it doesn't work. I've... (2 Replies)
Discussion started by: jwheeler
2 Replies

8. 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

9. UNIX for Dummies Questions & Answers

Padding

Hi Can anyone tell me how to pad zeroes on the left side to a numeric string in unix shell scripting Your answer is very much appreciated Thanks Vijay (2 Replies)
Discussion started by: vijaygopalsk
2 Replies

10. Programming

Zero Padding to a string

I am writing a C program which a part of it needs to padding zero in front of a string. The program will get a sting from an ASCII file which the maxium length of this string is 5 char long. The string can sometimes less the 5 char long. In order to make it with the same length '0's are being... (3 Replies)
Discussion started by: Wing m. Cheng
3 Replies
Login or Register to Ask a Question