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

Getting rid of leading zeros

Status
Not open for further replies.

chomps303

MIS
Sep 30, 2003
83
0
0
US
I have a file with several fields. There is a field that has leading zeros, how can I get rid of them?

I am only showing 1 field below in sample.

Thanks in advance,

Brandt

ie:

"000000000501083"
"000000000546459"
"000000000006758"
"000000000314515"
"000000000332358"
"000000000082485"
"000000000328863"
"000000000402542"
"000000000143077"
"000000000058045"
"000000000475106"
"000000000551226"
"000000000441501"
"000000000000502"
"000000000479798"
"000000000231769"
"000000000000003"
"000000000259391
 
Assuming the first field as you didn't mention:
sed 's!"0*!"!' /path/to/input > output
Otherwise post more info on the input file.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
OK Thanks

I forgot to add that the field is NOT the first one.

It would be the 5th field

Thanks

Brandt

ie:

"P","A","","","000000000501083","0","aaaaaaa","bbbbbbbb","cccccccccc"
"P","A","","","000000000546758","0","bbbbbbb","cccccccc","cccccccccc"
"P","A","","","000000000314515","0","ccccccc","dddddddd","eeeeeeeeee"
"P","A","","","000000000332358","0","ddddddd","eeeeeeee","ffffffffff
 
awk 'BEGIN{FS=OFS=","}{sub(/"0*/,"\"",$5);print}' /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
 
Great!!

What would the syntax be for a second field say field 10?

Thanks

Brandt
 
Simply add another call to the sub function with the desired field number.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I can't seem to get the syntax correct.

I tried

awk 'BEGIN{FS=OFS=","}{sub(/"0*/,"\"",$5);sub(/"0*/,"\"",$16);print}' foo > foo.
out

field 5 works fine fild 16 does not.

Thanks

Brandt
 
Can you please post some input sample and given result ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
OK

field 5 & 13 take leading 0's out

thanks

"P","A","","","000000000501083","0","aaaaaaa","bbbbbbbb","cccccccccc","A","","","000000000546758","0","bbbbbbb","cccccccc"
"P","A","","","000000000546758","0","bbbbbbb","cccccccc","cccccccccc","A","","","000000000546758","0","bbbbbbb","cccccccc"
"P","A","","","000000000314515","0","ccccccc","dddddddd","eeeeeeeeee"","A","","","000000000546758","0","bbbbbbb","cccccccc"
"P","A","","","000000000332358","0","ddddddd","eeeeeeee","ffffffffff","A","","","000000000546758","0","bbbbbbb","cccccccc"


 
The code should work if you change $16 to $13.

Here's a version that removes 0's from every field, leaving one if the field had nothing but 0's.
Code:
# Remove leading zeros from each field.
# Input format: "xxxx","","xxxxxx","xx"
#   Set in and out field-separators to `","'.
BEGIN { FS=OFS="\",\"" }

# Work only on lines that begin & end with quote.
/^".*"$/  \
{ # Remove the quote at the beginning & at the end
  # of the line.
  gsub( /^"|"$/, "" )
  # Examine all fields.
  for (i=1; i<=NF; i++)
    if ( length( $i ) > 1 )
    { sub( /^0+/, "", $i )
      if ( ""==$i )
        # Leave 1 zero.
        $i = "0"
    }
  # Print with restored quotes.
  print "\"" $0 "\""
  next
}
# Print non-data line.
1
If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. For an introduction to Awk, see faq271-5564.

Let me know whether or not this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top