JPV
JPV

Reputation: 1079

unzip and process compressed data

I want to process zip files on the fly and I have an issue. When applying the following code.

for file in ./*.zip; do head -$(($(unzip -c $file | grep -n $channel_after | cut -f1,1 -d":"))) $file;done

The idea is to obtain all the lines up to $channel_after which is channel10. However I get the following output

PK;��L� ��p- marks.txtUT Q�K[Q�K[ux ��342�020��.(��QH��K�.J-�56���2�00��g(d$楧�X�[�q���fh`�+.�@�� -��̭�sRR�J�*A,���vQn�3��������r ,54���\PK;��L� ��p- ��marks.txtUTQ�K[ux

Whereas the original file looks like

12082008;pull done;ret=34;Y
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel8
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel9
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel10
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N

Why I am getting this error? Thanks

Upvotes: 0

Views: 54

Answers (2)

dhany1024
dhany1024

Reputation: 133

Your version is:

for file in ./*.zip; do head -$(($(unzip -c $file | grep -n $channel_after | cut -f1,1 -d":"))) $file;done

What you should do is:

for file in ./*.zip; do head -$(($(unzip -c $file | grep -n $channel_after | cut -f1,1 -d":"))) <(unzip -c $file);done

Upvotes: 1

choroba
choroba

Reputation: 241828

You're applying head to the original zipfile without unzipping it: the first argument to head is the arithmetic expansion with command substitution, the second one is $file. head doesn't know how to read zip files.

I saved the sample data as marks.txt and zipped it with

zip 1.zip marks.txt

I was then able to run

channel_after=Channel8
for file in 1.zip ; do
    head -$(($(unzip -c $file | grep -n $channel_after | cut -f1,1 -d":"))) \
        <(unzip -c $file)
done

which printed all the lines up to Channel8.

Upvotes: 1

Related Questions