mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

wikiaddfield (341B)


      1 #!/bin/bash
      2 
      3 FILE="$1" # full path to file
      4 FIELD="$2"
      5 VAL="$3"
      6 
      7 if grep "^$FIELD: " $FILE > /dev/null
      8 then
      9   sed -i "s/^$FIELD: .*/&$VAL/" $FILE
     10 else
     11   # Remove blank lines from the end of a file:
     12   # https://unix.stackexchange.com/a/552195
     13   sed -i -e :a -e '/^\n*$/{$d;N;ba' -e '}' $FILE
     14   echo >> $FILE
     15   echo "$FIELD: $VAL" >> $FILE
     16 fi
     17