Last active
April 11, 2018 19:45
-
-
Save v0lkan/b4401af94367282ad02e to your computer and use it in GitHub Desktop.
Maintains a textile of sorted unique lines.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# MIT Licensed | |
# Maintainer: Volkan Özçelik <[email protected]> | |
# 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