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

Similar documents
CS Unix Tools & Scripting Lecture 7 Working with Stream

CS Advanced Unix Tools & Scripting

CS Unix Tools & Scripting

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

Basics. I think that the later is better.

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

Module 8 Pipes, Redirection and REGEX

5/20/2007. Touring Essential Programs

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

Essential Linux Shell Commands

CS160A EXERCISES-FILTERS2 Boyd

Review of Fundamentals

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

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

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

Essentials for Scientific Computing: Stream editing with sed and awk

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

CS 3410 Intro to Unix, shell commands, etc... (slides from Hussam Abu-Libdeh and David Slater)

A Brief Introduction to the Linux Shell for Data Science

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

find starting-directory -name filename -user username

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Basic Linux (Bash) Commands

Pattern Matching. An Introduction to File Globs and Regular Expressions

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

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

Introduction To Linux. Rob Thomas - ACRC

Lab #2 Physics 91SI Spring 2013

UNIX / LINUX - REGULAR EXPRESSIONS WITH SED

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

CS 124/LINGUIST 180 From Languages to Information. Unix for Poets Dan Jurafsky

ITST Searching, Extracting & Archiving Data

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

- c list The list specifies character positions.

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

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

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

Systems Programming/ C and UNIX

Introduction To. Barry Grant

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

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Table Of Contents. 1. Zoo Information a. Logging in b. Transferring files 2. Unix Basics 3. Homework Commands

CSCI 2132: Software Development

5/8/2012. Exploring Utilities Chapter 5

Introduction to the Shell

CSE2031. Lab 2 FALL 2009


Pipelines, Forks, and Shell

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

CS Unix Tools & Scripting

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

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

CS Unix Tools & Scripting

STREAM EDITOR - REGULAR EXPRESSIONS

ADVANCED LINUX SYSTEM ADMINISTRATION

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

DATA 301 Introduction to Data Analytics Command Line. Dr. Ramon Lawrence University of British Columbia Okanagan

Why learn the Command Line? The command line is the text interface to the computer. DATA 301 Introduction to Data Analytics Command Line

Lecture 8: Structs & File I/O

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Today s Lecture. The Unix Shell. Unix Architecture (simplified) Lecture 3: Unix Shell, Pattern Matching, Regular Expressions

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.

GNU Bash. an introduction to advanced usage. James Pannacciulli Systems Engineer.


CSC UNIX System, Spring 2015

Reading and manipulating files

CSE 490c Autumn 2004 Midterm 1

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

do shell script in AppleScript

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

Overview of the UNIX File System

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Digital Humanities. Tutorial Regular Expressions. March 10, 2014

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

Mineração de Dados Aplicada

Welcome to the Bash Workshop!

Lab 2: Linux/Unix shell

CSE II-Sem)

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

EECS2301. Lab 1 Winter 2016

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

Regular Expressions. Michael Wrzaczek Dept of Biosciences, Plant Biology Viikki Plant Science Centre (ViPS) University of Helsinki, Finland

Scripting Languages Course 1. Diana Trandabăț

Unix. Examples: OS X and Ubuntu

Lec 1 add-on: Linux Intro

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

Shell Programming Overview

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

Overview of the UNIX File System. Navigating and Viewing Directories

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

18-Sep CSCI 2132 Software Development Lecture 6: Links and Inodes. Faculty of Computer Science, Dalhousie University. Lecture 6 p.

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

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

CS Unix Tools. Fall 2010 Lecture 10. Hussam Abu-Libdeh based on slides by David Slater. September 29th, 2010

The Unix Shell. Pipes and Filters

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

CS Unix Tools. Lecture 3 Making Bash Work For You Fall Hussam Abu-Libdeh based on slides by David Slater. September 13, 2010

Transcription:

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

Reasons to use Unix Reason #42 to use Unix: Wizardry Mastery of Unix makes you a wizard need proof? here is the profile page of the employees of a real estate company http://www.deansproperty.com.au/home/profiles can you spot the IT guy?

Sorting sort Sorts the lines of a text file alphabetically. sort -r u file sorts the file in reverse order and deletes duplicate lines. sort -n -k 2 -t : file sorts the file numerically by using the second column, separated by a colon Consider a file (numbers.txt) with the numbers 1, 5, 8, 11, 62 each on a separate line, then: $ sort numbers.txt $ sort numbers.txt -n 1 1 11 5 5 8 62 11 8 62

uniq uniq uniq file - Discards all but one of successive identical lines uniq -c file - Prints the number of successive identical lines next to each line

Search and Replace The Translate Command tr [options] <set1> [set2] Translate or delete characters Sets are strings of characters By default, searches for strings matching set1 and replaces them with set2 Example: echo somefile tr AEIOU aeiou vowels to lower case vowels - changes all capital

Redirection revisited tr only receives input from standard input (stdin) i.e. keyboard input What if we want to operate on files? 1 Piping: cat somefile tr AEIOU aeiou 2 Input redirection: tr AEIOU aeiou < somefile

Redirection revisited Input/Output Streams #0 : Standard input stream; STDIN (usually keyboard but can be redirected) to redirect standard input, use the < operator command < file #1 : Standard output stream; STDOUT (usually terminal console but can be redirected) to redirect standard output, use the > operator command > file #2 : Standard error stream; STDERR (depends on system setting, can also be redirected) to redirect standard error, use the > operator and specify the stream by number (2) command 2> file Can combine two streams together by using 2>&1 This says: send standard error to where standard output is going. Useful for debugging/catching error messages.

Redirection revisited Bash processes I/O redirection from left to right, allowing us to do fun things like this: Example: Let s delete everything but the numbers from test1.txt, then store them in test2.txt tr -cd 0-9 < test1.txt > test2.txt

Some Simple Examples Example: echo * prints everything in the directory, separated by spaces. Let s separate them by newlines instead: Example: echo * tr \n Let s print a file in all uppercase: replaces all spaces with newlines tr a-z A-Z < test.txt - prints the contents of text.txt in all caps

tr and sets If the two sets passed to tr are the same length then the first character in the first set is replaced by the first in the second and so on. If the second is shorter than the first, then they are matched and all extra are changed to the last character If the first is shorter, then only those corresponding characters in the second matter Example: echo "abcdefghijklmnopqrstuvwxyz" tr a-z a aaaaaaaaaaaaaaaaaaaaaaaaaa echo "abcdefghjiklmnopqrstuvwxyz" tr a-z wxyz wxyzzzzzzzzzzzzzzzzzzzzzzz

tr options tr has some very useful options: tr options tr -d <set> - delete all characters that are in the set tr -c <set1> [set2] - complements set1 before replacing it with set2 Example: Lets print a file with all non-letters removed: tr -cd a-za-z < somefile somefile - removes non-letters from

tr and character ranges tr does not understand regular expressions (and really for the task it is designed for they don t make sense), but it does understand characte ranges and POSIX character sets such as [:alpha:] Reminder: [:alnum:] [:alpha:] [:digit:] [:punct:] [:lower:] [:upper:] [:space:] - alphanumeric characters - alphabetic characters - digits - punctuation characters - lowercase letters - uppercase letters - whitespace characters

Substitution Cypher We can use tr to do a simple substitution cypher. Create a text file called cypher with some reordering of the alphabet. Then to encode we would just do tr a-z cat cypher < file > encodedfile and to decode we would do tr cat cypher a-z < encodedfile > decodedfile Note the use of backticks around cat cypher. By doing this the shell expands this command and passes the output to tr.

The Stream Editor sed sed is a stream editor. We will only cover the basics, as it is a completely programming language! Stream Editor sed [options] [script] [file] Straem editor for filtering and transforming text We will focus on sed s/<regexp>/<text> [file] This form replaces anything that matches <regexp> with <text>. sed goes line by line searching for the regular expression. What is the difference between sed and tr? sed can match regular expressions! sed also does lots of other stuff

Basic Example: Example: sed s/not guilty/guilty/g filename Replaces not guilty with guilty everywhere in the file What happens if we don t have the g?

Basic Example: Example: sed s/not guilty/guilty/g filename Replaces not guilty with guilty everywhere in the file What happens if we don t have the g? Without the g, it will only do one substitution per line.

sed deletion Just like with tr we can do deletion with sed sed deletion sed /regexp/d - deletes all lines that contain regexp Example sed /[Dd]avid/d filename > filename2 deletes all lines that contain either David or david and saves the file as filename2.

sed understands regular expressions! The power of sed is that it treats everything between the first pair of / s as a regular expression. So we could do sed s/[[:alpha:]]\{1,3\}[[:digits:]]*@cornell\.edu/cornell email removed/g file to print a file with all cornell email addresses removed. use -r to use extended regular expressions.

sed is greedy! sed matches a given regular expression to the longeset string as soon as possible: $ echo filename1 a b col d e f lapse h i j k lapse m n $ sed s/col.*lapse/collapse/g filename1

sed is greedy! sed matches a given regular expression to the longeset string as soon as possible: $ echo filename1 a b col d e f lapse h i j k lapse m n $ sed s/col.*lapse/collapse/g filename1 a b collapse m n $ echo filename2 a b c col d e f col g h i lapse sed s/col.*lapse/collapse/g filename2

sed is greedy! sed matches a given regular expression to the longeset string as soon as possible: $ echo filename1 a b col d e f lapse h i j k lapse m n $ sed s/col.*lapse/collapse/g filename1 a b collapse m n $ echo filename2 a b c col d e f col g h i lapse sed s/col.*lapse/collapse/g filename2 a b c collapse

sed can save the string Another Example: sed s/^\([a-z][a-za-z]*\), \([A-Z][A-Za-z]*\)/\2 \1/ filename Searches for an expression at the beginning of the line of the form e1, e2 where e1 and e2 are words starting with capital letters. Placing an expression inside ( ) tells the editor to save whatever string matches the expression Since ( ) are special characters we escape them; i.e. by using \( \) We access the saved strings as \1, \2. This script for example could convert a database file from Lastname, Firstname to Firstname Lastname

More sed You can specify which lines to check by numbers or with regular expressions: sed 1,20s/john/John/g filename - checks lines 1 to 20 sed /^The/s/john/John/g filename - checks lines that start with The & cooresponds to the pattern found: sed s/[a-z]\+/"&"/g filename replaces words with words in quotes For more on sed check out http://www.grymoire.com/unix/sed.html

Even more sed How could we use sed to remove a specific regular expression?

Even more sed How could we use sed to remove a specific regular expression? sed s/regexp/ /g file Example: sed s/[[:alnum:]]/ /g Frankenstein.txt

Examples: Let s strip the directory prefix from our pathnames (i.e. convert /usr/local/src to src Example: pwd sed s/.*// Translates anything preceeding (and including) a frontslash to nothing Note the backslash-escaped frontslash

sed scripting sed is a complete programming language and we can write sed scripts. Example Any file that begins with #! is a script file (we will talk more about this next week). Create a new text file named trim.sed #! /usr/bin/sed -f s/^ *// s/ *$// You can run this script from the shell like any other program: echo " this is a test "./trim.sed this is a test We now have a script that trims leading and trailing whitespace!

Sed Arkanoid Sed is a complete programming language. In fact people have written entire games as sed scripts. http:aurelio.net/soft/sedarkanoid/

Putting things together Example: We can put some of these together commands together now to do interesting things. history sort -k 2 sed s/^ *[0-9]\ *// uniq -c sort -n + Print out the history of commands with how many times each has been run