user331814
user331814

Reputation: 21

Remove all Leading zero from first column and if all zero found then Preserved 1 zero

I want to remove leading Zero from first column and if all Zeros are found in first column then 1 Zero should be preserved.

00123|a 
00154|b 
00000|c

Output

123|a 
154|b 
0|c

Upvotes: 1

Views: 271

Answers (3)

ctac_
ctac_

Reputation: 2491

If you want to use sed :

sed 's/^0*//;s/^|/0|/' infile

Upvotes: 2

James Brown
James Brown

Reputation: 37464

Using awk:

$ awk '
BEGIN { FS=OFS="|" }  # set field separators
{ $1+=0 }             # add a zero to remove leading zeros
1' file               # output

Output:

123|a 
154|b 
0|c

Here's one that works for integers:

$ awk '
BEGIN{ FS=OFS="|" }
{ $1=sprintf("%.0f",$1) }  # %.0f will round off decimals
1' file

Output:

123|a 
154|b 
0|c
4295229012|0

Upvotes: 1

Santhosh Kumar
Santhosh Kumar

Reputation: 543

Try

string="0000123456000" 
echo $((10#$string))

Upvotes: 0

Related Questions