Suppose I have the following syntax: HH:MM:SS
. I want to make it as short as possible. For example, I want "00:00:30" To turn into "30". Similarly, I want "0:30:00" to turn into "30:00". How can I do this with shell commands?
-
10What output you would expect with 00:00:00.– schrodingerscatcuriosityJun 22, 2022 at 22:35
1 Answer
As I understand you need to remove the leading zeroes, so let's suppose you have a file like this
00:00:30
00:01:30
00:30:00
01:30:00
30:00:00
would become
30
1:30
30:00
1:30:00
30:00:00
If that's so, you just need to apply a sed
command like this:
sed 's/^[0:]*//' file
-
11Consider adding
00:00:00
to the example. I read "as much as possible" and my formal brain totally enjoys what happens to this exact string. :D Jun 22, 2022 at 18:47 -
@KamilMaciorowski Good one :), I' d guess it would be 0... asked to OP. Jun 22, 2022 at 22:34
-
3Why, though? You would save a tiny amount of storage space and guarantee that your result was non-standard… Jun 22, 2022 at 23:14
-
7
-
2If you want the "00:00:00" → "0" case to be handled too: ` sed -e 's/^[0:]*//' -e 's/^$/0/'` - this will cause an empty string to be turned back into a single zero. There're probably more ways to achieve this.– BotOfWarJun 23, 2022 at 15:21