0

I have some text files the looks something like this:

Introduction and some meta data
[00:00.000 --> 00:04.380]  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[00:04.980 --> 00:07.200]  Sed mattis varius ligula vel egestas.

I want to count the characters but exclude the first line and the time stamp, that is, only count the characters in Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mattis varius ligula vel egestas.

The length of the timestamps varies (there might be hours as well, in the example above it is just minutes).

How do I do this?

2 Answers 2

1

One option is to break your problem down into pieces, then solve those pieces.

I'll use your sample input:

$ cat input
Introduction and some meta data
[00:00.000 --> 00:04.380]  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[00:04.980 --> 00:07.200]  Sed mattis varius ligula vel egestas.
  1. I want to skip the first line

    One option for that is to use tail:

    $ tail -n +2 input
    [00:00.000 --> 00:04.380]  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    [00:04.980 --> 00:07.200]  Sed mattis varius ligula vel egestas.
    

    The -n +2 means "print everything starting with line 2"

  2. I want to omit the timestamp

    One option for that is to use something like sed to trim it off:

    $ tail -n +2 input | sed 's/^\[[^]]*]  //'
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Sed mattis varius ligula vel egestas.
    

    The option to sed mean "substitute everything starting with [ that is not ] followed by ] (] followed by two spaces) with nothing."

  3. I want to get the character count

    Now with the first two pieces solved, getting the character count is a simple application of wc

    $ tail -n +2 input | sed 's/^\[[^]]*]  //' | wc -m
    95
    
0
0
echo "Introduction and some meta data
[00:00.000 --> 00:04.380]  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[00:04.980 --> 00:07.200]  Sed mattis varius ligula vel egestas." | sed -r "1d; s/^[^]]+]//" | wc -m 
99

or if the data is in a file FILE

sed -r "1d; s/^[^]]+]//" FILE | wc -m 
  • -r allows for bracket expressions and + without masquerade
  • 1d deletes the first line.
  • s substitutes, anchoring at start of line, substitutes everything up to the closing bracket with nothing.
  • wc -m counts characters

You must log in to answer this question.

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