Gentoo Archives: gentoo-user

From: David Haller <gentoo@×××××××.de>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT scripting - strip zero if between period and digit
Date: Tue, 22 Jan 2019 10:31:18
Message-Id: 20190122025515.55jw6r4mtu2bvavs@grusum.endjinn.de
In Reply to: Re: [gentoo-user] OT scripting - strip zero if between period and digit by Michael Orlitzky
1 Hello,
2
3 On Mon, 21 Jan 2019, Michael Orlitzky wrote:
4 >On 1/21/19 6:50 PM, Adam Carter wrote:
5 >> I need to clean up a file which has IP addresses with leading zeros in
6 >> some of the octets so I need to make, say, .09 into .9
7 >>
8 >> How do i do that in sed/awk/whatever?
9 >
10 >The first thing you should do is construct a bunch of test cases, with all of
11 >the possible input representations and what you think the output
12 >representation should be. Then, you should write a program in something other
13 >than bash that passes all of the test cases. It's not as easy as it sounds;
14 >for example:
15 >
16 > * What happens to 0.1.2.3?
17 >
18 > * What happens to 01.2.3.4?
19 >
20 > * What happens to 1.2.3.0?
21 >
22 > * What happens to 1.2.000.3?
23 >
24 >You need a parser, not a regular expression. (You can do it with a regex, but
25 >it's going to be one of those comical twelve-page-long things.)
26
27 $ printf '0.1.2.3 01.2.3.4 1.2.3.0 1.2.000.3\n' | \
28 sed 's/0*\([[:digit:]]\+\)/\1/g'
29 0.1.2.3 1.2.3.4 1.2.3.0 1.2.0.3
30
31 HTH,
32 -dnh
33
34 --
35 printk(KERN_DEBUG "adintr: Why?\n");
36 linux-2.6.19/sound/oss/ad1848.c

Replies