Computing.Net > Forums > Unix > Replace leading zeroes with spaces

Replace leading zeroes with spaces

Reply to Message Icon

Original Message
Name: pnbalaji
Date: June 15, 2008 at 22:29:24 Pacific
Subject: Replace leading zeroes with spaces
OS: Aix 5.3
CPU/Ram: 8GB RAM
Model/Manufacturer: IBM
Comment:

Hi,

I have a text file containing some numeric fields which are delimited by one or more spaces. Some of the numeric fields contain leading zeroes in it. I need to replace the leading zeroes with spaces in order to maintain the record layout.

An example file layout is given below.

Number Name Pay Tax Total
1 John 01000 005 01005
2 Pipal 00500 005 00505

Can some one help me? Is it possible to acheive this with awk or sed or tr easily?

Thanks,
Balaji.


Report Offensive Message For Removal


Response Number 1
Name: ghostdog
Date: June 15, 2008 at 22:43:24 Pacific
Reply: (edit)


awk '{
$3=$3+0
$4=$4+0
$5=$5+0}
1' file


Report Offensive Follow Up For Removal

Response Number 2
Name: nails
Date: June 16, 2008 at 09:38:04 Pacific
Reply: (edit)

Ghostdog:

What is the significance of the '1' at the end of the awk script? Is this short hand for 'print $0'?


Report Offensive Follow Up For Removal

Response Number 3
Name: ghostdog
Date: June 16, 2008 at 11:21:06 Pacific
Reply: (edit)

yes it is.


Report Offensive Follow Up For Removal

Response Number 4
Name: pnbalaji
Date: June 16, 2008 at 21:06:04 Pacific
Reply: (edit)

Hi,

Thanks for your response. I just noticed that values are not preceded with leading zeroes, but also with +. The contents of the file is as below.

Number Name Pay Tax Total
1 John +01000 +005 +01005
2 Pipal +00500 +005 +00505

How can I retain the + symbol, but remove the leading zeroes?

Thanks,
Balaji.


Report Offensive Follow Up For Removal

Response Number 5
Name: nails
Date: June 17, 2008 at 08:10:01 Pacific
Reply: (edit)

First, skip the header record; then, foreach numeric field, place the plus sign back:


# stub script only contains field 3
awk '{
if(NR == 1)
{
print $0
continue
}
$3=$3+0;
$3="+"$3


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Replace leading zeroes with spaces

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge