blob: 710da40585ac2c90eb2bf76d468e409821bb4949 [file] [log] [blame]
David Brazdil2200a1b2018-05-05 12:07:38 +01001#!/bin/bash
2set -e
3if [ -z "$1" ]; then
4 source_list=/dev/stdin
5 dest_list=/dev/stdout
6else
7 source_list="$1"
8 dest_list="$1"
9fi
10# Load the file
11readarray A < "$source_list"
12# Sort
13IFS=$'\n'
David Brazdilae88d4e2018-09-06 14:46:55 +010014# Stash away comments
Mathew Inwood6be0fdd2018-09-14 15:19:10 +010015C=( $(grep -E '^#' <<< "${A[*]}" || :) )
16A=( $(grep -v -E '^#' <<< "${A[*]}" || :) )
David Brazdilae88d4e2018-09-06 14:46:55 +010017# Sort entries
David Brazdil2200a1b2018-05-05 12:07:38 +010018A=( $(LC_COLLATE=C sort -f <<< "${A[*]}") )
19A=( $(uniq <<< "${A[*]}") )
David Brazdilae88d4e2018-09-06 14:46:55 +010020# Concatenate comments and entries
21A=( ${C[*]} ${A[*]} )
David Brazdil2200a1b2018-05-05 12:07:38 +010022unset IFS
23# Dump array back into the file
Paul Duffinc78bea42018-10-16 14:28:26 +010024if [ ${#A[@]} -ne 0 ]; then
Mathew Inwood50dea422018-10-05 15:47:29 +010025 printf '%s\n' "${A[@]}" > "$dest_list"
26fi