7

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?

1

1 Answer 1

21

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
18
  • 11
    Consider 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
  • 3
    Why, 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
    Non-sortable as well. Jun 23, 2022 at 8:35
  • 2
    If 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.
    – BotOfWar
    Jun 23, 2022 at 15:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .