1755657 Members
3838 Online
108837 Solutions
New Discussion юеВ

remove zero from values

 
SOLVED
Go to solution
N Gopal
Frequent Advisor

remove zero from values

Hi,

I have one file(myfile.txt) that contains values like:

"575129","0.2x0.4","0001","2"
"575129","0.2x0.4","0002","2"
"575129","0.4x0.3","0003","2"
"575129","0.4x0.3","0004","2"
"575129","0.3x0.4","0005","2"


i am trying to group values from Field 3 e.g "0002" ....

CODE i am using is :

a=`grep "575129" myfile.txt | cut -f6 -d'"'`
surface=`echo $a | awk '{out=$1;for (k=2;k<=NF;k++){out=sprintf("%s,%s",out,$k);}print out}'`

value of surface will be: 00001,00002 ...

i do not want to take 0's means only 1,2,3 ...

Can some one please help me in this regard.

Thanks
8 REPLIES 8
AwadheshPandey
Honored Contributor
Solution

Re: remove zero from values

add following line in your code.
echo $surface|sed 's/0//g'

Regards, Awadhesh
It's kind of fun to do the impossible
VK2COT
Honored Contributor

Re: remove zero from values

Hello,

If I understand you correctly, you
want a simple result like 1,2,3,4,5,

Many ways to achieve this. One of them
is by using two standard Shell tools:

sed -e 's/"//g' myfile.txt | awk -F"," '/575129/ {printf("%d,", $3)}'

Cheers,

VK2COT
VK2COT - Dusan Baljevic
Steven Schweda
Honored Contributor

Re: remove zero from values

> echo $surface|sed 's/0//g'

Brilliant. Shows thoughtful testing.

dyi # surface='00100'
dyi # echo $surface | sed 's/0//g'
1

I'm sure that that's what he wants.

Perhaps you meant something more like this?

echo $surface | sed 's/^0*//'
N Gopal
Frequent Advisor

Re: remove zero from values

Thanks Awadesh,

Your reply solved my problem.

Thanks to all.
AwadheshPandey
Honored Contributor

Re: remove zero from values

Sorry. I did a mistake.

but you u follow above expert advise of my friend after looking all aspects you will again a looser.
echo $surface
0001,0002,0003,0004,00050
echo $surface | sed 's/^0*//'
1,0002,0003,0004,00050

the more generic code will be
echo $surface|sed 's/^0*//g'|sed 's/,0*/,/g'

cheers,

Awadhesh
It's kind of fun to do the impossible
N Gopal
Frequent Advisor

Re: remove zero from values

I have got solution to my problem.

Thanks a lot.
AwadheshPandey
Honored Contributor

Re: remove zero from values

Thanks Gopal. I can assume that the person written code below(you)

a=`grep "575129" myfile.txt | cut -f6 -d'"'`
surface=`echo $a | awk '{out=$1;for (k=2;k<=NF;k++){out=sprintf("%s,%s",out,$k);}print out}'`

can do some testing also and can remove bugs also if it is there in any forum members reply.

:)

Regards,
Awadhesh
It's kind of fun to do the impossible
N Gopal
Frequent Advisor

Re: remove zero from values

Many Thanka