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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pre-pending to the begining of a line 1

Status
Not open for further replies.

JasonParr

Programmer
Jun 11, 2001
8
0
0
US
I have been attempting to pad the beginning of a line with zeros. the first field in a line is a number with the length of 1 - 6. I would like to put zeros in as place holders so that feild looks as though it is right justified and has a set width of 6 characters in length. The file is variable length and can get quite large and I need to be able to do this to all the lines. My AWK is very rusty any help is much appreciated. Thank you in advance.
-Jason
 
Hi Jason,

This should do what you want.

{
if (length($1) < 6) {
print length($1)
$0 = substr(&quot;000000&quot;,1,6-length($1)) $0
}
print
}

Hope this helps.
CaKiwi
 
Hi Jason,

Sorry, I inadvertently left a debug print statement in my last post. It should be

{
if (length($1) < 6) {
$0 = substr(&quot;000000&quot;,1,6-length($1)) $0
}
print
}

Hope this helps. CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top