简体   繁体   English

linux - 将具有不同行条目的 txt 文件转换为.csv?

[英]linux - Convert txt file with entries on different lines to .csv?

I have used exiftool to make.txt files with the EXIF data of some images and videos I am working with.我使用 exiftool 制作了包含我正在使用的一些图像和视频的 EXIF 数据的 .txt 文件。 I will also eventually need to create.csv manifests for these files, and I know there are simple ways to convert.txt files to.csv files, but the instructions I've found have described how to do so if the.txt file has the information to go in different columns within the same line, while mine are on different lines.我最终还需要为这些文件创建 .csv 清单,我知道有一些简单的方法可以将 .txt 文件转换为 .csv 文件,但我发现的说明已经描述了如果 .txt 文件有go 的信息在同一行内的不同列中,而我的信息在不同的行上。 Is there a way to do this conversion with.txt files that are organized differently?有没有办法用组织不同的 .txt 文件进行这种转换?

For example, I have seen instructions for how to convert something like this例如,我已经看到了如何转换这样的东西的说明

filename   date       size
abc.JPG    1/1/2001   1MB
def.JPG    1/1/2001   1MB
hij.JPG    1/1/2001   1MB

to

filename,date,size
abc.JPG,1/1/2001,1MB
def.JPG,1/1/2001,1MB
hij.JPG,1/1/2001,1MB

The.txt files I have, on the other hand, are formatted like this:另一方面,我拥有的 .txt 文件的格式如下:

========abc.JPG
File Name            abc.JPG
Date/Time Original   2001:01:01 1:00:00
Size                 1 MB
========def.JPG
File Name            def.JPG
Date/Time Original   2001:01:01 1:01:00
Size                 1 MB
========hij.JPG
File Name            hij.JPG
Date/Time Original   2001:01:01 1:02:00
Size                 1 MB

but I still need an output like但我仍然需要一个 output 之类的

filename,date,size
abc.JPG,2001:01:01 1:00:00,1 MB
def.JPG,2001:01:01 1:01:00,1 MB
hij.JPG,2001:01:01 1:02:00,1 MB

Using sed and Miller , you can run使用 sed 和Miller ,您可以运行

<input.txt sed -r 's/==.+//g;s/([a-zA-Z]) ([a-zA-Z])/\1-\2/' | mlr --x2c label filename,date,size >output.csv

to have具有

filename,date,size
abc.JPG,2001:01:01 1:00:00,1 MB
def.JPG,2001:01:01 1:01:00,1 MB
hij.JPG,2001:01:01 1:02:00,1 MB

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

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