Gentoo Archives: gentoo-user

From: "François-Xavier CARTON" <fx.carton@×××××.fr>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT scripting - strip zero if between period and digit
Date: Wed, 23 Jan 2019 04:36:55
Message-Id: 5b8206c0-6cee-74ad-c44c-409cacb6fd8c@yahoo.fr
1 Le 23/01/2019 à 04:19, Adam Carter a écrit :
2 > > François-Xavier
3 > >
4 > >
5 >
6 > My bad, it should be:
7 >
8 > sed 's/0*\([0-9][0-9]*\)/\1/g'
9 >
10 > (tests are indeed needed!)
11 >
12 >
13 > Many thanks François. This is almost right, but it is also stripping
14 > zeros that follow a letter, and I only want it to strip zeros that are
15 > proceeded by a period. There are no leading zeros in the first octet of
16 > the IP so that case does not need to be handled.
17 >
18 > Does the \1 refer to what's in the ()'s? So anything that one would wont
19 > to carry through should be inside the ()'s and anything that's outside
20 > is stripped, right?
21 >
22 >
23 >
24
25 Yes, \1 is the content in (). But adding letters inside won't solve the
26 problem, eg. "a01" will still be changed to "a1".
27
28 AFAIK, there is no way to express "start of line or a character" in sed,
29 but you could do two regexps, one starting with ^ (start of line), the
30 other with \. (dot)
31
32
33 sed 's/^0*\([0-9][0-9]*\)/\1/g;s/\.0*\([0-9][0-9]*\)/.\1/g'