| 1 |
#!/usr/bin/env sh |
| 2 |
|
| 3 |
# Description: Splits the file passed as argument or joins selection |
| 4 |
# |
| 5 |
# Note: Adds numeric suffix to split files |
| 6 |
# Adds '.out suffix to the first file to be joined and saves as output file for join |
| 7 |
# |
| 8 |
# Shell: POSIX compliant |
| 9 |
# Authors: Arun Prakash Jana, ath3 |
| 10 |
|
| 11 |
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection} |
| 12 |
resp=s |
| 13 |
|
| 14 |
if [ -s "$selection" ]; then |
| 15 |
printf "press 's' (split current file) or 'j' (join selection): " |
| 16 |
read -r resp |
| 17 |
fi |
| 18 |
|
| 19 |
if [ "$resp" = "j" ]; then |
| 20 |
if [ -s "$selection" ]; then |
| 21 |
arr=$(tr '\0' '\n' < "$selection") |
| 22 |
if [ "$(echo "$arr" | wc -l)" -lt 2 ]; then |
| 23 |
echo "joining needs at least 2 files" |
| 24 |
exit |
| 25 |
fi |
| 26 |
for entry in $arr |
| 27 |
do |
| 28 |
if [ -d "$entry" ]; then |
| 29 |
echo "can't join directories" |
| 30 |
exit |
| 31 |
fi |
| 32 |
done |
| 33 |
|
| 34 |
file="$(basename "$(echo "$arr" | sed -n '1p' | sed -e 's/[0-9][0-9]$//')")" |
| 35 |
sort -z < "$selection" | xargs -0 -I{} cat {} > "${file}.out" |
| 36 |
|
| 37 |
# Clear selection |
| 38 |
if [ -p "$NNN_PIPE" ]; then |
| 39 |
printf "-" > "$NNN_PIPE" |
| 40 |
fi |
| 41 |
fi |
| 42 |
elif [ "$resp" = "s" ]; then |
| 43 |
if [ -n "$1" ] && [ -f "$1" ]; then |
| 44 |
# a single file is passed |
| 45 |
printf "split size in MB: " |
| 46 |
read -r size |
| 47 |
|
| 48 |
if [ -n "$size" ]; then |
| 49 |
split -d -b "$size"M "$1" "$1" |
| 50 |
fi |
| 51 |
fi |
| 52 |
fi |