loretoparisi
loretoparisi

Reputation: 16271

use split -d on macOS bash

When on linux split -d can be used as a numeric suffixes starting at 0, so like having

split -d -l 1500 ${TEMP_FILE} ${OUTPUT_FILE}
    mv "${DATADIR}/${DATASET}/user_artists00" "${DATADIR}/${DATASET}/user_artists.train"
    mv "${DATADIR}/${DATASET}/user_artists01" "${DATADIR}/${DATASET}/user_artists.test"

but on macOS the -d option it is missing. According to man we have

 -a suffix_length
             Use suffix_length letters to form the suffix of the file name.

and it is stated

If additional arguments are specified, the first is used as the name of the input file which is to be split. If a second additional argument is speci- fied, it is used as a prefix for the names of the files into which the file is split. In this case, each file into which the file is split is named by the prefix followed by a lexically ordered suffix using suffix_length characters in the range ``a-z''. If -a is not specified, two letters are used as the suffix.

so in my understanding I cannot have as output file something like $MYINPUT00, $MYINPUT01, etc, while only xaa, xab, etc. by defaults, since -a only admits a [a-z] range.

Upvotes: 2

Views: 4644

Answers (2)

Zeynep Akkalyoncu
Zeynep Akkalyoncu

Reputation: 875

You can install brew install coreutils with Homebrew and invoke the GNU split gsplit instead to use the -d argument.

Upvotes: 8

D. Jones
D. Jones

Reputation: 481

macOS split is BSD split, which is different than GNU split. -d is an extension of GNU split. So you cannot use numeric suffixes with BSD split.

However, if you really want to do it on macOS, you can write a simple additional script to make it work.

split -l "$linelimit" "$infile" "$prefix"
i=0
# to put "suffixlen" digits at the end
suffixlen=3
for file in "$prefix"*; do
    # to make 2 as 002 etc.
    suffix=$(printf "%0${suffixlen}d" $i)
    # actual renaming
    mv "$file" "$prefix$suffix"
    ((i++))
done

Upvotes: 4

Related Questions