Skip to content

Instantly share code, notes, and snippets.

@v0lkan
Last active April 11, 2018 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save v0lkan/b4401af94367282ad02e to your computer and use it in GitHub Desktop.
Save v0lkan/b4401af94367282ad02e to your computer and use it in GitHub Desktop.
Maintains a textile of sorted unique lines.
#!/usr/bin/env bash
# MIT Licensed
# Maintainer: Volkan Özçelik <me@volkan.io>
# A quick and dirty way to keep a sorted text file of things.
#
# Usage:
#
# ./add 'lorem ipsum' 'dolor' 'dolor' 'dolor' 'ipsum dolamet' 'john doe'
#
# And when you `cat ./lines.txt` you'll get:
#
# dolor
# ipsum dolamet
# john doe
# lorem ipsum
#
# That is, there won’t be any duplicate lines on the files, and the file
# will be sorted at all times as long as you use this script.
#
# Not rocket surgery, but it’s scratching a certain itch of mine nonetheless :)
#
# Suggestions to make it better are welcome.
# Create the file if it does not exist:
touch lines.txt
# These are temporary buffers:
touch .tmp
touch .tmps
echo -n "" > .tmp
# Copy all arguments to the first buffer,
# taking care of the arguments that have spaces in them too:
for i in `seq 1 $#`
do
eval a=\$$i
echo "${a}" >> .tmp
done
# Copy what we already have, sort and dedupe:
cat lines.txt >> .tmp
cat .tmp | sort -u > .tmp2
# Backup the current file, just in case:
TS=`date +%s`
cp lines.txt lines.txt."${TS}"
# Replace the newly sorted data into `lines.txt`:
rm lines.txt
mv .tmp2 lines.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment