Skip to content

Instantly share code, notes, and snippets.

@izabera
Created March 31, 2015 02:31
Show Gist options
  • Save izabera/4c9dd28a67edd81479b0 to your computer and use it in GitHub Desktop.
Save izabera/4c9dd28a67edd81479b0 to your computer and use it in GitHub Desktop.
rearrange columns in a csv file
#!/bin/bash
# csvmove
# rearrange columns of a csv file read from stdin
# usage: csvmove column [column...]
# column arguments can be formatted like this: 1,3,3,7-10,32
# any column can be repeated more than once or skipped
if (( ! $# )); then
echo "Missing argument" >&2
exit 1
fi
# expand a range
for range; do
IFS=, read -ra input <<< "$range"
for i in "${input[@]}"; do
if [[ $i =~ ^[[:digit:]]+-[[:digit:]]+$ ]]; then
eval "nums+=({${i%-*}..${i#*-}})"
elif [[ $i =~ [[:digit:]]+ ]]; then
nums+=("$i")
else
echo "Invalid number or range: $i" >&2
exit 1
fi
done
done
# needs gnu awk for FPAT
awk '
BEGIN {
FPAT = "([^,]*)|(\"[^\"]+\")"
for (i = 1; i < ARGC; i++) column[++j] = ARGV[i]
ARGC = 1
}
{
for (i = 1; i < j; i++) printf ("%s,", $column[i])
print $column[i]
}
' "${nums[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment