2019-06-20 08:45:24 +00:00
|
|
|
#!/usr/bin/env sh
|
2019-06-06 17:57:57 +00:00
|
|
|
|
|
|
|
# Description: Splits the file passed as argument or joins selection
|
|
|
|
#
|
|
|
|
# Note: Adds numeric suffix to split files
|
2019-06-20 08:45:24 +00:00
|
|
|
# Adds '.out suffix to the first file to be joined and saves as output file for join
|
2019-06-06 17:57:57 +00:00
|
|
|
#
|
2019-06-20 08:45:24 +00:00
|
|
|
# Shell: POSIX compliant
|
2019-06-21 17:06:34 +00:00
|
|
|
# Authors: Arun Prakash Jana, ath3
|
2019-06-06 17:57:57 +00:00
|
|
|
|
2020-04-24 04:42:24 +00:00
|
|
|
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
|
2019-06-20 08:45:24 +00:00
|
|
|
resp=s
|
2019-06-06 17:57:57 +00:00
|
|
|
|
2019-06-20 08:45:24 +00:00
|
|
|
if [ -s "$selection" ]; then
|
2019-11-21 20:44:25 +00:00
|
|
|
printf "press 's' (split current file) or 'j' (join selection): "
|
|
|
|
read -r resp
|
2019-06-20 08:45:24 +00:00
|
|
|
fi
|
2019-06-06 17:57:57 +00:00
|
|
|
|
|
|
|
if [ "$resp" = "j" ]; then
|
|
|
|
if [ -s "$selection" ]; then
|
2019-06-20 08:45:24 +00:00
|
|
|
arr=$(tr '\0' '\n' < "$selection")
|
|
|
|
if [ "$(echo "$arr" | wc -l)" -lt 2 ]; then
|
|
|
|
echo "joining needs at least 2 files"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
for entry in $arr
|
|
|
|
do
|
|
|
|
if [ -d "$entry" ]; then
|
|
|
|
echo "cant join directories"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
done
|
2019-06-06 17:57:57 +00:00
|
|
|
|
2019-06-20 08:45:24 +00:00
|
|
|
file="$(basename "$(echo "$arr" | sed -n '1p' | sed -e 's/[0-9][0-9]$//')")"
|
|
|
|
sort -z < "$selection" | xargs -0 -I{} cat {} > "${file}.out"
|
2019-06-06 17:57:57 +00:00
|
|
|
fi
|
|
|
|
elif [ "$resp" = "s" ]; then
|
2019-06-20 08:45:24 +00:00
|
|
|
if [ -n "$1" ] && [ -f "$1" ]; then
|
2019-06-06 17:57:57 +00:00
|
|
|
# a single file is passed
|
2019-11-21 20:44:25 +00:00
|
|
|
printf "split size in MB: "
|
|
|
|
read -r size
|
2019-06-06 17:57:57 +00:00
|
|
|
|
2019-06-20 08:45:24 +00:00
|
|
|
if [ -n "$size" ]; then
|
2019-06-06 17:57:57 +00:00
|
|
|
split -d -b "$size"M "$1" "$1"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|