William Everett
William Everett

Reputation: 781

How can I use awk to modify this field

I am using awk to create a .cue sheet for a long mp3 from a list of track start times, so the input may look like this:

01:01:00-Title-Artist
01:02:00:00-Title2-Artist2

Currently, I am using "-" as the field separator so that I can capture the start time, Artist and Title for manipulation.

The first time can be used as is in a cue sheet. The second time needs to be converted to 62:00:00 (the cue sheet cannot handle hours). What is the best way to do this? If necessary, I can force all of the times in the input file to have "00:" in the hours section, but I'd rather not do this if I don't have to.

Ultimately, I would like to have time, title and artist fields with the time field having a number of minutes greater than 60 rather than an hour field.

Upvotes: 0

Views: 95

Answers (2)

William Pursell
William Pursell

Reputation: 212298

fedorqui's solution is valid: just pipe the output into another instance of awk. However, if you want to do it inside one awk process, you can do something like:

awk 'split($1,a,":")==4 { $1 = a[1] * 60 + a[2] ":" a[3] ":" a[4]}
    1' FS=- OFS=- input

The split works on the first field only. If there are 4 elements, the pattern re-writes the first field in the desired output.

Upvotes: 3

fedorqui
fedorqui

Reputation: 289855

Like this, for example:

$ awk -F: '{if (NF>3) $0=($1*60+$2)FS$3FS$4}1' file
01:01:00-Title-Artist
62:00:00-Title2-Artist2

In case the file contains 4 or more fields based on : split, it joins 1st and 2nd with the rule 60*1st + 2nd. FS means field separator and is set to : in the beginning.

Upvotes: 3

Related Questions