Skip to main content

CSC 282.01, Class 04: The Bash shell - a refresher

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Questions
  • Refresher: Some basic tasks
  • What makes a good solution?
  • Sample solutions
  • Bash

News / Etc.

  • Sorry for not getting homework out last week. It was more chaotic than I thought it would be. (It also sounded like some of you needed a break.)

Upcoming Work

Forthcoming

Good things to do

  • Today’s CS Extra on the 4-1 program
  • Swim meet, all weekend
  • Final home basketball games of the year
  • Art house event Saturday at 1pm
  • Concert Saturday at 2pm with “Sound painting”
  • Slavic Coffee House Saturday at 5:30 pm in Bucksbaum Rotunda
  • Tuesday’s CS Table on Net Neutrality
  • Today, 11am, pay Noteworthy to serenade people
  • Monday, bone marrow drive

Questions

Refresher: Some basic tasts

  • Given a DOS-formatted text file (lines end with \r\n rather than just \n), convert it to a standard text file.
  • Given a standard text file, convert all uppercase letters to lowercase.
  • Given a standard text file, remove all blank spaces at the end of lines.
  • Make a list of all misspelled words in a text file.
  • Given a CSV file in which each line has the form
    LastName,FirstName,Assignment,NumericGrade find the the five highest grades on homework 2.
  • Given an HTML file, find the URLs of all images. In case you don’t know HTML, those will typically look like
    <img ... src="*URL*" ...>
    • The img can have any capitalization (img, IMG, Img, iMg, etc.)
    • There can be other text between the img and the src. (That text cannot include a greater than sign.)
    • You may find it easier to start this problem by assuming that there’s only one image tag on a line.

What makes a good solution?

  • It works correctly
  • As little work for the programmer as possible
  • Efficient
  • Clear
  • Modifiable
  • Elegant / clever / not garbage
  • You can learn from it

Some sample solutions

Given a DOS-formatted text file (lines end with \r\n rather than just \n), convert it to a standard text file.

  • Backstory: Typewriters
  • Useful tool: od, octal dump, look at the contents of files
  • Solution one: Write a C program
    • Twenty minutes to an hour for student
    • Five minutes Sam (actually, 1:30 for a mediocre version)
  • Solution two: In vi, use :1,$s/\r\n/\n/g
    • :1,$ means every line
    • s mean substitute
    • / is the separator
    • \r\n is the pattern
    • \n is the replacement
    • g is “everywhere”
    • Didn’t work
    • Didn’t work with sed. Why not?
    • Debugging: Long time
  • Solution three: R (using a scripting language)
    • Load the file into a string
    • Did a search and replace
    • Saved
    • About five minutes
  • Solution four: tr - translates or deletes characters
    • tr -d \r
    • One minute

Given a standard text file, convert all uppercase letters to lowercase.

  • Solution one: C program
    • Five minutes to fifteen minutes
  • Solution two: Python (or other scripting language)
    • Two minutes
  • Solution three: tr
    • tr [A-Z] [a-z] < oldfile > newfile
    • tr [:upper:] [:lower:] < oldfile > newfile
    • Why upper and lower? More readable.
    • :upper: and :lower: support i18n.

Given a standard text file, remove all blank spaces at the end of lines.

  • Solution one: C program
    • Whoops! Buffer length! (Someone will always find a way to overflow your buffer.)
    • Thirty minutes
  • Solution two: vim plugin
    • Install whitespace plugin
    • Save file
    • Five minutes
  • Solution three: emacs
    • M-x white-cleanup
    • Five seconds
  • Solution four: Scripting language
    • Replace ‘ \n’ with ‘\n’
    • Three minutes
  • Solution five: vi search and replace
    • :1,$s/ *$//
    • Ten seconds
  • Solution six: sed search and replace
    • sed is “stream editor”, it’s a non-interactive editor. You give it editing commands and it modifies things

Given a CSV file in which each line has the form
LastName,FirstName,Assignment,NumericGrade find the the five highest grades on homework 2.

  • Solution one: Load it into Excel, etc.
  • Solution two: R or scripting language
    • Read CSV file
    • Subset by things that have column 3 labeled HW2
    • Sort
    • Thirty seconds
  • Solution three: C program
  • Solution four: cat file | grep ',HW2,' | sort -t, -n -k3 | cut -f4 | head -5 Given an HTML file, find the URLs of all images. In case you don’t