简体   繁体   English

如何使用awk修改此字段

[英]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: 我正在使用awk从曲目开始时间列表中为长mp3创建一个.cue工作表,因此输入内容可能如下所示:

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). 第二次时间需要转换为62:00:00(提示表无法处理小时)。 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. 如有必要,我可以在小时部分将输入文件中的所有时间都强制设置为“ 00:”,但我宁愿不要这样做。

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. 最终,我希望拥有时间,标题和艺术家字段,其中时间字段的分钟数大于60,而不是小时的分钟数。

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 . 如果文件包含基于: split的4个或更多字段,它将以规则60*1st + 2nd连接1st和60*1st + 2nd FS means field separator and is set to : in the beginning. FS表示字段分隔符,一开始设置为:

fedorqui's solution is valid: just pipe the output into another instance of awk. fedorqui的解决方案是有效的:只需将输出通过管道传递到awk的另一个实例中。 However, if you want to do it inside one awk process, you can do something like: 但是,如果要在一个awk流程中执行此操作,则可以执行以下操作:

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. 如果有4个元素,则该模式会在所需输出中重写第一个字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM