Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk, nawk??? 2

Status
Not open for further replies.

mwesticle

Programmer
Nov 19, 2003
51
US
Hi. I have a fixed width file, that is 62 bytes wide. The first 50 bytes are irrelevant to me. Bytes 51-62 are what I'm interested in, here. Starting in position 51, there is a number that is one to 12 bytes wide. I want to alter the file, padding any non-12-byte number with zeros, to fill out the 12 bytes. So, here's what the file looks like (four record sample):

'[Byte1-50]1234 '
'[Byte1-50]12345 '
'[Byte1-50]123456 '
'[Byte1-50]123456789012'

After running some command (awk, nawk...I don't know...that's the info I'm looking for here), I need the file to look like this:

'[Byte1-50]000000001234'
'[Byte1-50]000000012345'
'[Byte1-50]000000123456'
'[Byte1-50]123456789012'

Anyone think they know how to do this? I'm assuming it's probably not too difficult, I just don't know what the command would be. Thanks in advance!
 
awk '{print substr($0,1,50)sprintf("%012d",substr($0,51,12))}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
A bit shorter:
[tt]
{printf "%s%012d\n",substr($0,1,50),substr($0,51)}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top