Command Line Bash

Examples of using Bash on the command line



Compare two unsorted lists

# Looks for all lines in `second-file.txt`, which don't match
# any line in `first-file.txt`.
grep -Fxv -f first-file.txt second-file.txt


Loop over an array of ints

for i in {1,2,3}; do echo ${i}; done

for i in {1,2,3}; do echo "$i"; done


Loop over a range of ints

for i in {1..10}; do echo ${i}; done

for i in {1..10}; do echo "$i"; done


Loop over an array of strings

for s in a b c d e; do echo "$s"; done


Run command on an interval

Every 1 seconds


while sleep 1; do [cmd]; done

while sleep 1; do echo "Hello"; echo "World"; done

while true; do clear; ./runScript.sh; sleep 1; done

Every 1 milliseconds


while sleep 0.1; do [cmd]; done

while sleep 0.1; do echo "Hello"; done


Run command on interval with incrementing variable

Every 1 seconds


i=1; while true; do echo "out - $i"; ((i++)); sleep 1; done
While loop with a multi-line string

Use a Here String to effectively create a for loop with multiline data


while read i
do
  echo "test$i"
done <<< 'a
b
c'


AWK

Print the 5th column


echo "This is a string made of columns" | awk '{print $5}'

Print the 5th column, with quotation marks, using printf


echo "This is a string made of columns" | awk '{printf("\"%s\"", $5);}'

Print all columns after, and including, the 5th column


echo "This is a string made of columns" | awk '{ s = ""; for (i = 5; i <= NF; i++) s = s $i " "; print s }'

Change the field seperator


echo "This\is\a\string\with\a\different\seperator" | awk -F'\' '{print $4}'

Pad filename with zeros

Change spaces to "-" and pad the number at the end with 4 zeros


echo "This is a file name 3" | awk '{printf "%s-%s-%s-%s-%s-%05d", $1, $2, $3, $4, $5, $6}'


Sed

Command Description
sed '/^$/d' Remove blank lines
sed '/^ /d' Remove lines starting with two spaces
sed 's/^textToRemove//g' Remove text
echo "text::to::remove [TECAN,Text to Keep]" | sed "s/text::to::remove \[TECAN,//" | sed "s/\]//" Remove text


Grep

Command Description
make run | grep --line-buffered -i "thing1\|thing2" Grep a stream of data for thing1 or thing2, case-insensitively