zero padding problem (bash)

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting zero padding problem (bash)
# 1  
Java zero padding problem (bash)

Hi there,

I need to loop some values,

for i in $(seq $first $last)
  does something here

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  
$> read first
$>printf "%05d\n" $first

# 3  
for i in {00001..99999}
     does something here

for i in $(seq -w ${first} ${last})
     does something here

# 4  

Similarly to c:
#!/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

for (( i=first ; i<=last ; i++ ))
  printf "%05i\n" $i

exit 0

% ./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


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

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #323
Difficulty: Easy
In most programming languages, the operator ++ is equivalent to the statement -= 1;.
True or False?

10 More Discussions You Might Find Interesting

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

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

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

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

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. UNIX for Dummies Questions & Answers

Zero padding dates

I have a file with records containing dates like: SMPBR|DUP-DO NOT USE|NEW YORK||16105|BA5270715|2007-6-6|MWERNER|109||||JOHN||SMITH|MD|72211118||||||74559|21 WILMINGTON RD||D|2003-11-6|SL# MD CONTACT-LIZ RICHARDS|||0|Y|N||1411458| How can I get the date fields in each of my records to be... (1 Reply)
Discussion started by: ChicagoBlues
1 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. Shell Programming and Scripting

Padding in Unix

I have a file with different character counts on each line how do i make it with unique character counts. example: 1st line : ABCD 011 XYZ 0000 YYYY BBB TEADINGDA 2nd line: ABCD 011 xys 0010 YYYY BBB TEAD 3rd line : ABCD 022 YXU 000 UUU BBB TE 1st line is 43... (3 Replies)
Discussion started by: rudoraj
3 Replies

9. UNIX for Dummies Questions & Answers


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

Featured Tech Videos