The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Padding with zeros. sbasetty Shell Programming and Scripting 10 05-18-2009 07:09 PM
Triml leading zeros in unix kingofprussia UNIX for Dummies Questions & Answers 7 08-07-2008 05:04 AM
how to retain leading zeros Manish Jha Shell Programming and Scripting 3 11-09-2006 05:59 PM
Leading zeros wtofu Shell Programming and Scripting 1 09-16-2006 02:52 PM
Padding zeros after removing commas in file pranag21 HP-UX 1 11-09-2005 10:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-13-2008
jakSun8 jakSun8 is offline
Registered User
  
 

Join Date: Dec 2007
Location: Gotham City
Posts: 59
Help needed in padding leading zeros

Hi all,
I have file with numeric values. I need to pad each value with leading zeros such that total lenght of each value is 16.

Example:
cat tmp.txt
502455
50255
5026
5027
5028

Output
0000000000502455
0000000000050255
0000000000005026
0000000000005027
0000000000005028

Any help is appreciated
Thanks,
Jak
  #2 (permalink)  
Old 03-13-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
Code:
# awk '{printf "%015s\n", $0}' file
000000000502455
000000000050255
000000000005026
000000000005027
000000000005028
  #3 (permalink)  
Old 03-13-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Code:
printf "%.16d\n" $(<tmp.txt)
  #4 (permalink)  
Old 03-13-2008
jakSun8 jakSun8 is offline
Registered User
  
 

Join Date: Dec 2007
Location: Gotham City
Posts: 59
Thanks guys!
Jak
  #5 (permalink)  
Old 07-01-2008
jakSun8 jakSun8 is offline
Registered User
  
 

Join Date: Dec 2007
Location: Gotham City
Posts: 59
How do i pad the hexadecimal values with leading zeroes

hex=`echo "obase=16;$data" | bc`
paddedData=`printf "%s" $hex `
echo $paddedData

results in
0
1
9
A

i want
00
01
09
0A

Any help is appreciated
JAK
  #6 (permalink)  
Old 07-02-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 560
Try like this:

Code:
"/" > x=A
"/" > echo $x | awk '{printf "%02s\n",$0}'
0A
"/" > x=9
"/" > echo $x | awk '{printf "%02s\n",$0}'
09
  #7 (permalink)  
Old 07-02-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
paddedData=`printf "%02x" 0x$hex `
Closed Thread

Bookmarks

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0