-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
split-csv.bash
executable file
·67 lines (56 loc) · 1.9 KB
/
split-csv.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
# Script Name: split-every-two.bash
# Purpose: Splits a comma-separated list into pairs and outputs each pair on a new line
# Created Date: Nov 07, 2024
# Author: [Your Name]
# Required Packages: None
# Powered by: [HomeSetup](https://github.com/yorevs/homesetup)
# GPT: [HHS-Script-Generator](https://chatgpt.com/g/g-ra0RVB9Jo-homesetup-script-generator)
################################################################################
# AIs CAN MAKE MISTAKES.
# For your safety, verify important information and code before executing it.
#
# This program comes with NO WARRANTY, to the extent permitted by law.
################################################################################
VERSION="0.0.1" # https://semver.org/ ; major.minor.patch
# @purpose: Display version information
version() {
echo "split-every-two.bash version ${VERSION}"
}
# @purpose: Display usage information
usage() {
echo "Usage: split-every-two.bash <comma-separated-list>"
echo "Example: split-every-two.bash 'item1,item2,item3,item4,item5,item6'"
}
# Check if the user needs help
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
usage
exit 0
fi
# Check if the user needs version information
if [[ "$1" == "-v" || "$1" == "--version" ]]; then
version
exit 0
fi
# @purpose: Splits a comma-separated list into pairs and prints each pair on a new line.
# @param $1 [Req] : Comma-separated list.
split_every_two() {
local input_list=$1
local -a items
IFS=',' read -r -a items <<< "$input_list"
for ((i=0; i<${#items[@]}; i+=2)); do
if [[ $((i+1)) -lt ${#items[@]} ]]; then
echo "${items[i]},${items[i+1]}"
else
echo "${items[i]}"
fi
done
}
# Check if an argument was provided
if [[ -z "$1" ]]; then
echo -e "\033[31mERROR\033[m: No comma-separated list provided."
usage
exit 1
fi
# Run the main function
split_every_two "$1"