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
Help needed in padding leading zeros jakSun8 Shell Programming and Scripting 12 07-02-2008 07:41 PM
Byte Padding naan High Level Programming 6 02-22-2007 05:09 AM
Padding zeros after removing commas in file pranag21 HP-UX 1 11-09-2005 10:22 PM
Padding vijaygopalsk UNIX for Dummies Questions & Answers 2 06-27-2003 10:51 AM
Padding issues informshilpa UNIX for Advanced & Expert Users 2 03-01-2002 01:51 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-06-2007
sbasetty sbasetty is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 75
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 00850 TX


Thanks in advance for your help
  #2 (permalink)  
Old 02-07-2007
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Code:
#! /bin/ksh

typeset -RZ 6 code

while IFS=':' read first rest
do
    code=${first}
    echo "${code}: ${rest}"
done < input.txt
  #3 (permalink)  
Old 02-07-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
while IFS=':' read first rest
do
    printf "%06d:%s\n" $first "$rest"
done< file
  #4 (permalink)  
Old 02-07-2007
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Another way:
Code:
awk -F: '{ printf "%06d: %s\n", $1,$2 }' input.txt
  #5 (permalink)  
Old 05-13-2009
limshady411 limshady411 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Arizona
Posts: 10
Padding with zeros

Hi all,

Referencing the awk statement here
Code:
awk -F: '{ printf "%06d: %s\n", $1,$2 }' input.txt
, I want to apply this concept to the 5th field, where I am padding 6 leading zeroes, and the rest of my fields stay the same. I tried the following:

Code:
awk -F, '{ printf "%06d, %s\n", $6,$7 }' test1.csv > test2.csv
but it won't output my first five fields or fields after the 6th field.

Please advise. Thanks much!
Lim
  #6 (permalink)  
Old 05-13-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
Code:
 echo "1 2 3 4 5 6 7 8 9" | awk '{ $6=sprintf("%06s", $6); print $0}'
1 2 3 4 5 000006 7 8 9
  #7 (permalink)  
Old 05-13-2009
limshady411 limshady411 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Arizona
Posts: 10
Thanks Jim. This looks like it would do the trick. I can't get it working with my code though. I started using this:
Code:
while IFS=',' read first rest
do
  printf "%07d,%s\n" $first "$rest" >> newfile.csv
done< file.csv
which will pad the first field. But I would like to pad the second field. How do I get the code above to pad the second? Thanks much!

Here's the input file:
"100","100","ABC"
"100","200","ABC"
"100","300","ABC"

Here's the desired output:
"100","0000100","ABC"
"100","0000200","ABC"
"100","0000300","ABC"
Sponsored Links
Reply

Bookmarks

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 09:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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