2

Using unzip I'm attempting to extract the first 1000 lines from an xml file. From reading How to partially extract zipped huge plain text file? I've used the answers to create:

unzip -p my_feed.zip | dd count=1000 > out.txt 

which is close to what I'm trying to achieve.

man dd 

describes :

count=n Copy only n input blocks. 

What is the size of an input block ? How to extract the first 1000 lines instead of first 1000 input blocks ?

Update:

I forgot to mention I'm using osx. I thought this would achieve same result:

gunzip < my_feed.zip | head -n 1000 

but returns error:

gunzip: unknown compression format 
5
  • 1
    @roaima please see update. Commented Mar 16, 2020 at 23:09
  • dd does not work with lines, only raw bytes. Please use file my_feed.zip and post the report it gives. GNU gunzip should work with zip files on Linux, but only if they contain a single file compressed with 'deflation' mode. Commented Mar 16, 2020 at 23:39
  • 2
    unzip will normally unpack files onto disk, so you can't pipe them into head. You might try unzip on one file to stdout: unzip -c my_feed.zip desired_file | head -n 1000 > out1000.txt Commented Mar 16, 2020 at 23:44
  • 1
    @Paul_Pedant why do need 'desired_file' ? unzip -c my_feed.zip | head -n 1000 > out1000.txt should suffice ? Commented Mar 17, 2020 at 20:19
  • zip files are archives -- they can contain multiple files. Using the -c or -p option outputs all the files to stdout. As you have not confirmed the actual type of my_feed.zip (as shown by file), or that there is a single file (as shown by unzip -tv), then I suggest you specify a single filename from the archive. gunzip should work with a single file, so I assume the file is either multi-file or corrupt. I note your OP used unzip with dd, and then gunzip with head, and wondered why you didn't try unzip with head anyway. Commented Mar 17, 2020 at 21:55

1 Answer 1

1

Use

zcat myfile.zip | head -n 1000 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.