How to Bash Your Plain Text Foes



How to Bash Your Plain Text Foes

0 0


how-to-bash-your-plain-text-foes

How to Bash your Plain Text Foes

On Github mimming / how-to-bash-your-plain-text-foes

How to Bash Your Plain Text Foes

Credit: xkcd.com/149/

Jenny Tong

Developer Advocate at Google google.com/+mimming@MimmingCodes

Jenny interrogates you

  • About your shell experience: none? a little? lots?
  • Have you programed before?

Agenda

  • Getting around the with bash
  • Copying stuff
  • Philosophy of POSIX tools
  • Finding files
  • Regular expressions: How do they work?
  • Working with text

Agenda

Credit: Allie

Getting around

# Where am I?
$ pwd
# What's here?
$ ls
$ ls -al
# Go somewhere
$ cd foo
# Go back
$ cd ..
# Directory stack
$ pushd foo
$ popd
# Make directory
$ mkdir foo

Magic stuff

  • up arrow to get previous commands
  • tab completes things
  • ctrl + a and ctrl + e move around the line

File stuff

# Copy a file
$ cp foo.txt bar.txt
# Move a file
$ mv foo.txt bar.txt
# Download a file
$ curl http://example.com > foo.html

Exercise

There are some plays here: http://mimming.com/presos/how-to-bash-your-plain-text-foes/shakes/

Make a directory for them Download all of them Bonus: unzip complete.zip

Philosophy break

Philosophy of POSIX tools

Philosophy of POSIX tools

  • Worse is better
  • Simple tools. Combine them.
  • The magic pipe | It glues stuff together
$ cat foo.txt | grep bar

Figuring out a tool

# The help parameter
$ ls --help
ls: illegal option -- -
usage: ls [-ABCFGHLOPRSTUW...] [file ...]
# Google and StackOverflow
$ open https://www.google.com/#q=ls+bash
# man pages
$ man ls
# Waaay too much info
# The 'q' key exits

Exercise

There is a strange command called yes. Figure out what it does.

Finding stuff

# Display file contents
$ cat macbeth.txt
# Filter lines
$ grep 'Thou' macbeth.txt
# OR
$ cat macbeth.txt | grep 'Thou'
# Find files
$ find ./
$ find ./ | grep 'mac'

Exercise

  • Display lines in Macbeth that contain 'thou'
  • Find all downloaded files that contain 'a' in their name (hint: you need both find and grep)
  • Display lines in Macbeth that contain 'sycamore' or 'evermore'

Regular expressions

a.k.a. fancy matchers

  • [0-9]
  • [a-zA-Z]
  • [tT]hou
  • ACT [IVX]*
  • .

Execrise!

  • Display lines in Macbeth that contain 'thou' or 'Thou'
  • Bonus: Display lines that contain a tab character

Working with text

  • cat - file to stream
  • egrep - enhanced grep but with regex - work with lines
  • tr - character swapper
  • sed - run regexes on a stream
  • awk - column manipulator

Working with text

  • wc - count stuff
  • sort - sorts stuff (and uniques)
  • more - look before you leap
  • xargs - turn standard output into arguments
  • > - stream to a file

All the stuff at once exercises

  • count the words in Shakespeare's complete works
    • remove numbers
    • remove punctuation
  • Count the number of words that end in 'ed' in Shakespeare's complete works
  • Use history command to count number of times you used the 'cd' command
  • Your requests!

THE END

These slides

mimming.com/presos/how-to-bash-your-plain-text-foes

Contact me

google.com/+mimming

@MimmingCodes

Questions?