Lecture 3 Tonight we dine in shell. Hands-On Unix System Administration DeCal

Similar documents
Module 8 Pipes, Redirection and REGEX

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Review of Fundamentals

STATS Data Analysis using Python. Lecture 15: Advanced Command Line

BASH SHELL SCRIPT 1- Introduction to Shell

Systems Programming/ C and UNIX

ITST Searching, Extracting & Archiving Data

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Basics. I think that the later is better.

Essentials for Scientific Computing: Stream editing with sed and awk

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Introduction to UNIX. Introduction. Processes. ps command. The File System. Directory Structure. UNIX is an operating system (OS).

Introduction to UNIX. CSE 2031 Fall November 5, 2012

Basic Linux (Bash) Commands

CS 25200: Systems Programming. Lecture 11: *nix Commands and Shell Internals

IB047. Unix Text Tools. Pavel Rychlý Mar 3.

Shell Programming Overview

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

UNIX Shell Programming

Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CSCI 2132 Software Development. Lecture 7: Wildcards and Regular Expressions

Unix Processes: C programmer s view. Unix Processes. Output a file: C Code. Process files/stdin: C Code. A Unix process executes in this environment

About this course 1 Recommended chapters... 1 A note about solutions... 2

Unix Processes. A Unix process executes in this environment

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

A Brief Introduction to the Linux Shell for Data Science

Scripting Languages Course 1. Diana Trandabăț

Week Overview. Simple filter commands: head, tail, cut, sort, tr, wc grep utility stdin, stdout, stderr Redirection and piping /dev/null file

Part III. Shell Config. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

Linux Text Utilities 101 for S/390 Wizards SHARE Session 9220/5522

Mineração de Dados Aplicada

Unleashing the Shell Hands-On UNIX System Administration DeCal Week 6 28 February 2011

Common File System Commands

FILTERS USING REGULAR EXPRESSIONS grep and sed

Advanced Linux Commands & Shell Scripting

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

Regular Expressions. Regular expressions match input within a line Regular expressions are very different than shell meta-characters.

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

CS 301. Lecture 05 Applications of Regular Languages. Stephen Checkoway. January 31, 2018

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Regex, Sed, Awk. Arindam Fadikar. December 12, 2017

Wildcards and Regular Expressions

Shells and Shell Programming

9.2 Linux Essentials Exam Objectives

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

CST Algonquin College 2

Useful Unix Commands Cheat Sheet

5/8/2012. Exploring Utilities Chapter 5

Introduction to UNIX Part II

Regular Expressions 1

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Environment Variables

File Commands. Objectives

CST Lab #5. Student Name: Student Number: Lab section:

Output with printf Input. from a file from a command arguments from the command read

User Commands sed ( 1 )

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

Vi & Shell Scripting

Advanced training. Linux components Command shell. LiLux a.s.b.l.

Bash Script. CIRC Summer School 2015 Baowei Liu

Lab 2: Linux/Unix shell

5/20/2007. Touring Essential Programs

INTRODUCTION TO SHELL SCRIPTING ITPART 2

Unix Internal Assessment-2 solution. Ans:There are two ways of starting a job in the background with the shell s & operator and the nohup command.

Regular Expressions. Regular Expression Syntax in Python. Achtung!

A shell can be used in one of two ways:

CS Unix Tools. Fall 2010 Lecture 5. Hussam Abu-Libdeh based on slides by David Slater. September 17, 2010

find Command as Admin Security Tool

Shells and Shell Programming

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

Consider the following program.

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

The input can also be taken from a file and similarly the output can be redirected to another file.

Session: Shell Programming Topic: Advanced Commands

Principles of Bioinformatics. BIO540/STA569/CSI660 Fall 2010

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

Fall 2006 Shell programming, part 3. touch

bash, part 3 Chris GauthierDickey

Pattern Matching. An Introduction to File Globs and Regular Expressions

CSE 374 Programming Concepts & Tools. Laura Campbell (thanks to Hal Perkins) Winter 2014 Lecture 6 sed, command-line tools wrapup

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

More regular expressions, synchronizing data, comparing files

psed [-an] script [file...] psed [-an] [-e script] [-f script-file] [file...]

Pattern Matching. An Introduction to File Globs and Regular Expressions. Adapted from Practical Unix and Programming Hunter College

More text file manipulation: sorting, cutting, pasting, joining, subsetting,

CS214-AdvancedUNIX. Lecture 2 Basic commands and regular expressions. Ymir Vigfusson. CS214 p.1

Introduction to UNIX. Introduction EECS l UNIX is an operating system (OS). l Our goals:

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

CSE 390a Lecture 7. Regular expressions, egrep, and sed

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

CS395T: Introduction to Scientific and Technical Computing

CSC UNIX System, Spring 2015

Shells & Shell Programming (Part B)

Introduction to Unix Week 3

CS Unix Tools & Scripting

Transcription:

Lecture 3 Tonight we dine in shell Hands-On Unix System Administration DeCal 2012-09-17

Review $1, $2,...; $@, $*, $#, $0, $? environment variables env, export $HOME, $PATH $PS1=n\[\e[0;31m\]\u\[\e[m\]@\[\e[1;34m\]\w \ [\e[2;90m\]\@\n\[\e[m\]\[\e[0;35m\]\h\[\e[m\]\ [\e[0;31m\]\$\[\e[m\]\[\e[0;32m\] quotes, ' and " aliases globbing backticks (`) pipes ( )

tee essentially a pipe mostly used to do $ sudo <command> sudo tee <file>

find google search on steroids for file system regexes, depths, mtimes, types, groups, users, etc. $ find -L /usr/ports/packages -type l -exec rm -- {} + $ find / -newer ttt -user wnj -print $ find a b -type f! -name 'crazy' -printf '%f\n'

xargs most used after a find: -print0-0 $ find. -name 'randomcrapfile' xargs rm $ find. -name 'filetobemoved' xargs -I {} mv {} somefolder used with find to print NUL character generally for xargs used in xargs in conjunction with -print0 for `find' for files with spaces

locate cached google search for file system precompiled database faster than `find', but not as detailed in search

for and while loops built into shell Structure: for {something} do somecommand someothercommand done One-liner, with semi-colons: $ for {something}; do somecommand; someothercommand; done

for and while loops Structure while {something} do somecommand someothercommand done One-liner, with semi-colons: $ while {some expression}; do somecommand; someothercommand; done

awk full programming language generally used to do (simple) regular expressions on files More info at: https://en.wikipedia.org/wiki/awk, http://www.grymoire.com/unix/awk.html

Moar shell-fu grep sed cut tr wc sort head tail

tr tr [OPTION]... SET1 [SET2] SET1 and SET2 define ordered sets of characters (characters of input that `tr' operates on) Function is to replace, squeeze, remove characters from its input No filenames to provide as arguments Reads stream from stdin, writes to stdout

tr tr [OPTION]... SET1 [SET2] Examples $ echo the quick brown fox tr abc def Replace characters in SET1 with corresponding characters in SET2 can use ranges (e.g, a-z A-Z) $ echo the quick brown fox tr a-z A-Z Commonly used options -d, --delete -s, --squeeze-repeats

sort sort [OPTION]... [FILE]... Useful options: -d, --dictionary-order Consider blanks and alphanumeric characters -n, --numeric-sort Sort by numerical value -r, --reverse Reverse the result of comparisons -f, --ignore-case -k(column), -nk2 means sort column 2 numerically

cut cut [OPTION]... [FILE]... Print selected parts of lines from each FILE (or stdin) to stdout Useful options: -d, --delimter=delim -f, --fields=list

head/tail View first/last parts of file Useful for viewing logs Default: view first/last 10 lines Common options -n, --lines=n Output first/last N lines, tail -f <file> follow the file, output appended data as <file> grows tail -n +N <file> or tail --lines=+n <file> Starting from N, output N to rest of file head --lines=-n <file> View everything but the last N lines in <file>

wc Word count Print newline, word, and byte counts wc -l, print newline count (count lines) wc -w, print word count

Regular Expressions (regex) String matching Set of metacharacters let you search for text that fits criteria Text editors, utilities, programming languages grep, sed, awk, vi(m) Perl, Ruby, etc. Many flavors, POSIX BRE Regex globs/wildcards Different sets of metacharacters used for different purposes Filename expansion vs. string matching The shell itself does not recognize RE's. It is the commands and utilities, that do.

Basic Regex \ (backslash) turn off special meaning of following character (escaping). (period) match any single character [..] (bracket expression) Matches ONE of any of the enclosed characters Hyphens indicate a range of characters (a-z, A-Z, 0-9) * (a quantifier) match any number or none of preceding character e.g, a* matches 'abc', 'bc' aa* matches 'abc' but not 'bc'

Anchors (regex) Specify where matching text should be ^ Match following regex at beginning of line $ Match preceding regex at end of line

Examples Regex Matches ------------------------------------------------------------------- tolstoy ^tolstoy tolstoy$ tolstoy, anywhere tolstoy, beginning of line tolstoy, end of line ^tolstoy$ a line containing exactly 'tolstoy' and nothing else [Tt]olstoy Either Tolstoy or tolstoy tol.toy tol, followed by any character, followed by toy Tol.*toy tol, any sequence of 0 or more characters, followed by toy

grep grep [OPTIONS] PATTERN [FILE...] Match text (PATTERN can be w/ or w/o regex) Grep (BRE), egrep/grep -E (ERE), fgrep/grep -F (match fixed strings) Can search with fixed strings, or with regexes Common options -i case insensitive search -l list names of files instead of printing the actual matching lines -v print lines that DON'T match the pattern -e <pattern> Use multiple -e options to search with multiple patterns

sed sed [OPTIONS] 'COMMAND' [FILE...] Stream editor for filtering and transforming text on an input stream (file or input from pipeline) Commonly used to perform text substitution in a pipeline 'COMMAND' is often substituting, appending, inserting, deleting text SUBSTITUTION: sed 's/old value/new value/(flags)' <file> 'old value' can be a regex

sed substitution sed 's/old [OPTIONS] value/new 'COMMAND' value/(flags)' [FILE...] <file> Common flags n replace nth instance of pattern with replacement g replace ALL instances of pattern with replacement Without flags, sed replaces first instance of 'old value' with 'new value' in each line $ echo I hate this decal sed 's/hate/love/' $ echo hi hi hi sed 's/hi/bye/'

sed: deletion sed '{what [OPTIONS] to find} 'COMMAND' d' <file> [FILE...] # deletion {what to find} can be: Range of lines: sed '1,3d' <myfile.txt> Regex: sed '/#/d' (delete comments maybe?) Other sed commands include insertion (i) and appending text (a)

Common options Many commands share some common options: -h/--help -v/--verbose -d/--debug -f/--force or file input -R recursive