Keval Shah
Keval Shah

Reputation: 393

Linux Shell Script to unzip and split file outputs unreadable files

I have a zip folder which contains multiple files of same format. Each file with around 50 mb size. I need to split each file into multiple chuncks (say 1000 lines per spllited output file).

I have written a shell script which which unzips the folder and saves the split files output in a directory.

The problem is that the output chunks are in unreadable format containing symbols and random characters. When I do it for each file individually, it outputs perfect txt split files. But it is not happening for whole zip folder.

Anyone knows how to can I get those files in txt format. Here is my script.

for z in input.zip ; do
    if unzip -p "$z" | split -l 1000 $z output_dir ; then
    echo "$z"
fi
done

Upvotes: 2

Views: 546

Answers (1)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84443

Problem

You need to unzip the files first. Otherwise, you are just chunking the original binary ZIP file.

Solution

The following is untested because I don't have your source file. However, it should work for you with a little tweaking.

unzip -d /tmp/unzipped input.zip
mkdir /tmp/split_files

for file in /tmp/unzipped/*txt do;
    split -l 1000 "$file" "/tmp/split_files/$(basename "$file" .txt)"
done

Upvotes: 1

Related Questions