Hi Freinds,
I have file1.txt as below
file1.txt
1521894~~-0.400~201207
1521794~~-0.486~201207
152494~~-0.490~201207
152154894~~-0.490~201207
1521894354~~-0.489~201207
expected output :
1521894~~-0.4~201207
1521794~~-0.486~201207
152494~~-0.49~201207
152154894~~-0.49~201207
1521894354~~-0.489~201207
Please help. I am very new to unix.
Try:
awk -F"~" -vOFS="~" '{sub("0*$","",$3)}1' file
1 Like
Lem
3
With sed:
sed 's/0*\(~[0-9]*\)$/\1/' <file
--
Bye
1 Like
@Lem ,@bartus11 thanks for reply. it worked good . but if i have a value 9.000 the output which i am getting is 9. but expcted is 9
RudiC
5
Building on @Lem's and @bartus11's solutions, try
awk -F"~" -vOFS="~" '{sub("[.0]*$","",$3)}1' file
or
sed 's/[\.0]*\(~[0-9]*\)$/\1/' <file
btw, Bartus11's suggestion will work if field4 contains e.g. letters while Lem's will fail on that.
1 Like
rdrtx1
6
An awk solution:
awk -f b.awk infile
where b.awk:
{
FS=l="~";
for (i=1; i<=NF; i++) {
if ($(i) ~ /[.]/) sub("[.0]*$", "", $(i));
if (i==NF) l="\n";
printf $i l;
}
}
Lem
7
This should work better:
sed 's/\.*0*\(~[^~]*\)$/\1/' <file
--
Bye
1 Like
@All thanks for all the suggestions. Would you mind giving the soultion to the other scenario where the field contains 0
example :
if i use
awk -F"|" -vOFS="|" '{sub("0*$","",$3)}1' file
In the output file it is removing the "0" from third field which should not be removed.
Plz help..
pamu
9
try this....
awk -F"|" -vOFS="|" '$3 !~ /^[0-0]+$/{sub("0*$","",$3)}1' file
awk '$3~/\./{sub(/\.?0+$/,x,$3)}1' FS=\| OFS=\| infile