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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Replace leading zeroes with spaces

Reply to Message Icon

Name: pnbalaji
Date: June 15, 2008 at 22:29:24 Pacific
OS: Aix 5.3
CPU/Ram: 8GB RAM
Product: 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.



Sponsored Link
Ads by Google

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


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


0

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

Ghostdog:

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


0

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

yes it is.


0

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

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.


0

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

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Replace leading zeroes with spaces

padding a number with leading zeros www.computing.net/answers/unix/padding-a-number-with-leading-zeros/5218.html

stripping leading zero www.computing.net/answers/unix/stripping-leading-zero/4753.html

replacing a character with a tab www.computing.net/answers/unix/replacing-a-character-with-a-tab/4966.html