Currently, my bash script splits by number of lines. However, I'd like to split a file into X pieces, each of those having total lines equal to the file length divided by X. The script is run as follows:
./script.sh input_file.tsv
So far, in the script, I have this:
INPUT_FILE=$1
SPLIT_NUM_THREADS=15
TOTAL_LINES=$(wc -l < $INPUT_FILE)
SPLIT_NUM=$( echo "scale=6; $TOTAL_LINES / $SPLIT_NUM_THREADS" | bc)
The following issues exist:
- Using $INPUT_FILE to get TOTAL_LINES gets me the error "ambiguous redirect", but using simply "input.tsv" does not. What's wrong there?
- SPLIT_NUM is a float, how do I convert it to an int so it can split by lines?
How can I resolve these issues and split a file by number of pieces?
echo "$INPUT_FILE"
before the line with the error (though I don't see a possible problem yet). – Hauke Laging Nov 19 '14 at 23:09SPLIT_NUM=$(expr '(' $TOTAL_LINES + $SPLIT_NUM_THREADS - 1 ')' / $SPLIT_NUM_THREADS )
. There are more compact ways to do this, depending on your shell. – Mark Plotnick Nov 19 '14 at 23:26