Zipping the files with data

I have files
a_cd_1.csv
a_cd_2.csv
a_cd_3.csv
I need to zip these files into one zip file a_cd.zip but if no data is there in any of these csv then only csv with data should be zipped.
Also these files will always have header record if data is present or not.
Please let me know how this scenario can be handled using unix script

You probably want to use the find command with -size +1c and pipe
that into tar, then gzip the tar file or just add the compress option to tar.

Is that a standard header with a standard length? If not, you need to count lines with e.g. wc -l and then go on like gandolf989 proposed.

Hi Rudic,

Yes it will be always standard header.
Eg:

a,b,c,d

I need to check the files whether it is having the data or not leaving the header and if those files has data then those files needs to zipped into one single file.

Please let me know.

Thanks,
Vinoth

You might want to use gandolf989's proposal but with -size +xc where x is the header's size. Or you might use stat -c%s , and when the result is larger than your header's size, add the file to the zip.

I tried both find and stat but its not working. It would be helpful if you can give the pesudo code so that I can try once.

Thanks.

What did you try with find? What did it do? How did it fail?

What did you try with stat? What did it do? How did it fail?

Your requirements are ambiguous. We may have a language barrier. If you show us sample files, we may be able to help you better.

Are you saying that a file should not be archived unless it contains more than 1 line? Or, are you saying that a file should not be archived unless it contains a correct header as its first line, and also contains data following the header?

How can we (or your script) determine whether or not the 1st line in a file is a "standard header"?

Hi Don,

I don't know whether I made the requirements clear. Sorry if I woudln't have done it.

I have files
abc_1.csv
abc_2.csv
abc_3.csv

These files should be zipped into one file abc.zip

All the three files will have standard header a,b,c,d even though data is present or not.

Say for an example out of 3 files above one file abc_2.csv is not having the data it should not be added to zip file.

So under this scenario my zip file should contain only abc_1.csv, abc_3.csv

Thanks.

Assuming that you're using a shell that recognizes basic POSIX shell constructs, you could try something like:

for i in a_cd_*.csv
do	if [ $(wc -l < "$i") -gt 1 ]
	then	printf "%s\n" "$i"
	fi
done | zip -@ a_cd

This zip utility is not covered by the standards and its options may vary from system to system. This was tested and works on Mac OS X.

1 Like

Don,

Thanks a lot for your help.

Code is working fine.

Thanks once again.