I have a tar file which has lot of csv files in it. How to get the first few lines of each csv file without extracting it?
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. Similar errors are reported for all csv files... Any idea??
Using -O
you can tell tar to extract a file to standard output instead of to file. 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 xf <YOUR_FILE> <NAME_OF_CSV> -O | head
to get the file's beginning to stdout. This may be a bit ineffective since you unpack the archive as many tiems as there are CSV files, but should work.
You can use perl and its Archive::Tar
module. 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
. Otherwise you will have to grep
the list to filter those you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.