Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Add csv script #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions bash/csv/csv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
#
# Pure Bash utilities to merge CSV files.
#

########################################################################
# Merge all csv files.
# Omit the first line of every file except the first,
# so the csv header is not repeated.
# Arguments:
# Input CSV files...
# Outputs:
# Empty string if no input files otherwise the csv lines.
########################################################################
function csv::merge () {
if [ $# -eq 0 ]; then
return 0
fi
csv::header "${1}"
while [ $# -ne 0 ]; do
csv::records "${1}"
shift
done
}

########################################################################
# Print only the first line.
# Arguments:
# Input CSV file.
# Outputs:
# First line of the file.
########################################################################
function csv::header () {
local -a records
mapfile -t -n 1 records < "${1}"
printf "%s\n" "${records[@]}"
}

########################################################################
# Print all files expect the first one.
# Arguments:
# Input CSV file.
# Outputs:
# All lines expect the first one.
########################################################################
function csv::records () {
local -a records
mapfile -t -s 1 records < "${1}"
printf "%s\n" "${records[@]}"
}
1 change: 1 addition & 0 deletions bash/fonction.sh
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export EDITOR=vim

fonction::os_script fonction
fonction::os_script java
fonction::os_script csv

if [ -s "${FONCTION_ROOT}/git-prompt/gitprompt.sh" ]; then
GIT_PROMPT_ONLY_IN_REPO=1
Expand Down