bash

Useful bash Pieces


  • Remove all non-digit characters from a string (source):

    echo toto.titi.12.tata.2.abc.def | tr -d -c 0-9

  • Insert a line into a file. Example inserts between lines 2 & 3, and assumes file is <=1000 lines long.

    oldfile=file.json
    sed -n 1,2p $oldfile>tempfile.json
    echo "put this text in new line" >>tempfile.json
    sed -n 3,1000p $oldfile>>tempfile.json
    mv tempfile.json $oldfile

For JSON manipulation:

  • Remove characters from end of string. Example is final 4.

    echo ${variable%????}
  • Pull the line containing “RepetitionTime” from a JSON file; keep characters after colon; strip the trailing comma:

    tr=`more $jsonfile | grep "RepetitionTime"`

    tr=`echo ${tr#*:} | sed 's/.$//'`