SED & AWK (inprogress)

# who -r | sed -e ‘s/^[[:space:]]*//’
run-level 3  2012-01-05 17:02

remove blank lines from file

jjest@canadark:~$ cat test
middle

end

====

jjest@canadark:~$ sed ‘/^$/d’ test > test.out
jjest@canadark:~$ cat test.out
middle
end

Insert text at beginning of files…


$ cat > test
monkey

$ sed '1i\#author: jjest@uw.edu' test > test.tmp; 
mv test.tmp test
#author: jjest@uw.edu
monkey

$ for file in $(find ./ -name '*sh' | awk '{print $1}'); do 
sed '1i\#author: jjest@uw.edu' $file > ${file}.tmp; mv ${file}.tmp $file; 
done

 

Add a line before the 4th line of the line.

Add a line “Cool gadgets and websites” before 4th line. “a” command inserts the line after match whereas “i” inserts before match.

$ sed '4 i\
> Cool gadgets and websites' thegeekstuff.txt

Awk – Rename files to Lower or Upper Case.

root@donkey:/tmp#
for file in $(ls -la ./ | awk '{print $8}'); do
mv $file $( echo $file | awk '{print tolower($0)}');
done;

root@donkey:/tmp# ls
keyring-hs5u3c mozilla-media-cache mu3gbhjz muszpyya orbit-jjest orbit-root virtual-root.mvfgzj

root@donkey:/tmp#
for file in $(ls -la ./ | awk '{print $8}');
do mv $file $( echo $file | awk '{print toupper($0)}');
done;

mv: `.’ and `./.’ are the same file
mv: cannot remove `..’: Is a directory

root@donkey:/tmp# ls
KEYRING-HS5U3C MOZILLA-MEDIA-CACHE MU3GBHJZ MUSZPYYA ORBIT-JJEST ORBIT-ROOT VIRTUAL-ROOT.MVFGZJ

trailer film The Lobster 2015

Bash Tutorial (in progress…)

[root@jackson cron.d]# list[0]=jkl
[root@jackson cron.d]# list[1]=jkl
[root@jackson cron.d]# echo ${list[@]}
jkl jkl jkl

[root@jackson libexec]# name=”jeremiah”
[root@jackson libexec]# name=”$name jester”
[root@jackson libexec]# echo $name
jeremiah jester

Bash Backup script: Run if CPU usage is below threshold!


#!/bin/bash

PREV_TOTAL=0
PREV_IDLE=0

###
#added by jj
###

#If less then cpu threshold than will
#perform backup, else loop until true.
CPU_THRESHOLD=5

#current date
DATE=$(/bin/date +%d-%m-%y)

####

while true; do
  CPU=(`cat /proc/stat | grep '^cpu '`) # Get the total CPU statistics.
  unset CPU[0]                          # Discard the "cpu" prefix.
  IDLE=${CPU[4]}                        # Get the idle CPU time.

  # Calculate the total CPU time.
  TOTAL=0
  for VALUE in "${CPU[@]}"; do
    let "TOTAL=$TOTAL+$VALUE"
  done

  # Calculate the CPU usage since we last checked.
  let "DIFF_IDLE=$IDLE-$PREV_IDLE"
  let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
  let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"
  #echo -en "rCPU: $DIFF_USAGE%  bb"

  # Remember the total and idle CPU times for the next check.
  PREV_TOTAL="$TOTAL"
  PREV_IDLE="$IDLE"

  # Wait before checking again.
  sleep 1

  #added by jj
  if [ "$DIFF_USAGE" -lt "$CPU_THRESHOLD" ]
        then
                echo "CPU usage below $CPU_THRESHOLD%. Backing up now.."
                sleep 2

                #Perform backup.
                tar -czvf /vol03/backups/stone-archive-$DATE.tar.gz /vol03 /homes /etc

                #quit script.
                echo "Backup is complete."

                #delete old archives if approximately a month old
                REMOVE=$(find /vol03/backups/ -name *.tar.gz -atime +28 -exec rm {} 😉

                exit
  fi
done