Bash solutions

8. Scripts, functions, and variables

swap file names

function swap() {
    if [ -e $1 ] && [ -e $2 ] ; then
        /bin/mv $2 $2.bak
        /bin/mv $1 $2
        /bin/mv $2.bak $1
    else
        echo at least one of these files does not exist ...
    fi
}

countfiles()

function countfiles() {
    if [ $# -eq 0 ]; then
        echo "No arguments given. Usage: countfiles dir1 dir2 ..."
        return 1
    fi
    for dir in $@; do
        echo in $dir we found $(find $dir -type f | wc -l) files
    done
}

10. Text manipulation

10.1

cat wellsInvisibleMan.txt | tr -d "[:punct:]" | tr '[:upper:]' '[:lower:]' | \
  sed 's/ /\'$'\n/g' | sed '/^$/d' | sort | uniq -c | sort -gr > frequency.txt

10.3

awk -F, '{print $1 "," $2}' cities.csv > populations.csv

10.4

awk -F, 'NR%10==2' cities.csv

copy every 10th file

find /project/def-sponsor00/shared/toyModel -type f | sort | awk 'NR%10==0'

archive every 20th file

There are many solutions:

# the downside of this solution is that it'll include paths (without the leading /) into the arhive
tar cvf toy.tar $(find /project/def-sponsor00/shared/toyModel -type f | sort | awk 'NR%20==0')
cd /project/def-sponsor00/shared/toyModel   # if you are allowed to cd into that directory
tar cvf ~/tmp/toy.tar $(find . -type f | sort | awk 'NR%20==0')
cd -
find /project/def-sponsor00/shared/toyModel -type f | sort | awk 'NR%20==0' > list.txt
tar cfz toy.tar --files-from=list.txt
/bin/rm list.txt

10.7

find . -type f | xargs /bin/ls -lSh | awk '{print $5 "  " $9}' | head -5