简体   繁体   English

如何从tar文件中的csv文件中提取前几行而不在Linux中提取它?

[英]How to extract first few lines from a csv file inside a tar file without extracting it in linux?

I have a tar file which has lot of csv files in it. 我有一个tar文件,其中包含很多csv文件。 How to get the first few lines of each csv file without extracting it? 如何不提取每个csv文件的前几行?

I tried: 我试过了:

$(tar -Oxf $tarfile $file | head -n "$NL") >> cdn.log

But got error saying: 但是有错误说:

time(http:index: command not found

This is some line in one of the csv files. 这是其中一个csv文件中的一行。 Similar errors are reported for all csv files... Any idea?? 所有csv文件都报告了类似的错误...任何想法吗?

Using -O you can tell tar to extract a file to standard output instead of to file. 使用-O可以告诉tar将文件提取到标准输出而不是文件中。 So you should be able to first use tar tf <YOUR_FILE> to list the files from archive and filter it using grep to find the CSV files, and then for each file use tar xf <YOUR_FILE> <NAME_OF_CSV> -O | head 因此,您应该能够首先使用tar tf <YOUR_FILE>列出存档中的文件,并使用grep对其进行过滤以找到CSV文件,然后对每个文件使用tar xf <YOUR_FILE> <NAME_OF_CSV> -O | head tar xf <YOUR_FILE> <NAME_OF_CSV> -O | head to get the file's beginning to stdout. tar xf <YOUR_FILE> <NAME_OF_CSV> -O | head开始获取文件的开始stdout。 This may be a bit ineffective since you unpack the archive as many tiems as there are CSV files, but should work. 这可能有点无效,因为您解压缩了归档文件,其中包含与CSV文件一样多的tiems,但是应该可以。

You can use and its Archive::Tar module. 您可以使用及其Archive::Tar模块。 Here a one-liner that extract the first two lines of each one: 这里是一个单线,可提取每行的前两行:

perl -MArchive::Tar -E '
    for (Archive::Tar->new(shift)->get_files) { 
        say (join qq|\n|, (split /\n/, $_->get_content, 3)[0..1]) 
    }
' file.tar

It assumes that the tar file only has text files and they are csv . 假定tar文件只有文本文件,并且它们是csv Otherwise you will have to grep the list to filter those you want. 否则,您将必须grep列表以过滤所需的列表。

暂无
暂无

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

相关问题 如何在不解压的情况下找到tar文件中文件的行数 - How to find the number of lines of the files inside a tar file without extracting 忽略文件Linux中的前几行和后几​​行 - Ignore first few lines and last few lines in a file Linux 打印 tar.gz 中每个文件的第一行而不提取 - Print the first line of each file inside a tar.gz without extracting 用其他文件的前几行替换前几行 - Replace first few lines with first few lines from other file 从CSV文件中提取具有uniq值的前3行 - extract first 3 lines with uniq values from CSV file Linux - 如何在不提取内容并再次应用 tar 的情况下重命名 .tgz 文件中的文件? - Linux - how to rename files within a .tgz file without extracting contents and applying tar again? 从tar文件中提取特定目录而不创建包含层次结构 - extracting specific dir from tar file without creating the containing hierarchy 如何在不提取内容的情况下重命名.tar.gz文件并在UBUNTU中创建新的.tar.gz文件? - How to rename .tar.gz file without extracting the contents and creating the new .tar.gz file in UBUNTU? 如何使用一些 Linux 命令复制一个巨型文件的前几行,并在其末尾添加一行文本? - How to copy the first few lines of a giant file, and add a line of text at the end of it using some Linux commands? 如何在Linux中从压缩文件中删除第一行和最后一行 - How to remove the first and last lines from a zipped file in linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM