Skip to content

Instantly share code, notes, and snippets.

@bassemZohdy
Created September 27, 2017 17:14
Show Gist options
  • Save bassemZohdy/fbbbbafd9a09f1a99da464c2bf5e5139 to your computer and use it in GitHub Desktop.
Save bassemZohdy/fbbbbafd9a09f1a99da464c2bf5e5139 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
set -x
MAP_FILE='./map'
DELIMITER=","
add (){
KEY=$1
VALUE=$2
echo Key=$KEY Value=$VALUE
KEY_EXITS=$(cat $MAP_FILE|cut -d $DELIMITER -f1|grep "^$KEY$"|wc -l)
if [[ $KEY_EXITS -eq 0 ]]
then
NEW_LINE=${KEY}${DELIMITER}${VALUE}
else
LINE=$(cat $MAP_FILE|grep "^$KEY$DELIMITER"|tail -1)
sed -i "/^$KEY$DELIMITER/d" $MAP_FILE
VALUE_EXITS=$(echo $LINE|egrep "${DELIMITER}${VALUE}${DELIMITER}|${DELIMITER}${VALUE}$"|wc -l)
if [[ $KEY_EXITS -eq 0 ]]
then
NEW_LINE=${LINE}${DELIMITER}${VALUE}
else
NEW_LINE=${LINE}
fi
fi
echo "$NEW_LINE" >>$MAP_FILE
}
get (){
KEY=$1
VALUE=($(cat $MAP_FILE|grep "^$KEY"|tail -1| cut -d $DELIMITER -f 2- | tr $DELIMITER ' '))
}
add "cars" "bmw";
get "cars"
echo ${VALUE[1]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment