I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright 2001-2003.

Slides:



Advertisements
Similar presentations
Regular Expressions (in Python). Python or Egrep We will use Python. In some scripting languages you can call the command “grep” or “egrep” egrep pattern.
Advertisements

Lecture 5  Regular Expressions;  grep; CSE4251 The Unix Programming Environment.
CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
CSCI 330 T HE UNIX S YSTEM Regular Expressions. R EGULAR E XPRESSION A pattern of special characters used to match strings in a search Typically made.
Regular Expressions grep
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
CS 497C – Introduction to UNIX Lecture 29: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 31: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/23.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Quotes: single vs. double vs. grave accent % set day = date % echo day day % echo $day date % echo '$day' $day % echo "$day" date % echo `$day` Mon Jul.
Regular Expressions. u A regular expression is a pattern which matches some regular (predictable) text. u Regular expressions are used in many Unix utilities.
Unix Files, IO Plumbing and Filters The file system and pathnames Files with more than one link Shell wildcards Characters special to the shell Pipes and.
Shell Script Examples.
Regular Language & Expressions. Regular Language A regular language is one that a finite state machine (fsm) will accept. ‘Alphabet’: {a, b} ‘Rules’:
Chapter 4: UNIX File Processing Input and Output.
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
System Programming Regular Expressions Regular Expressions
INFO 320 Server Technology I Week 7 Regular expressions 1INFO 320 week 7.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 8 Shell.
Regular expressions Used by several different UNIX commands, including ed, sed, awk, grep A period ‘.’ matches any single characters.X. matches any X.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
CIS 218 Advanced UNIX1 Advanced UNIX CIS 218 Advanced UNIX Regular Expressions.
BIF713 Additional Utilities. Linux Utilities  You have learned many Linux commands. Here are some more that you can use:  Data Manipulation (Reg Exps)
Pipes and Filters Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
Appendix A: Regular Expressions It’s All Greek to Me.
CSC 4630 Meeting 21 April 4, Return to Perl Where are we? What is confusing? What practice do you need?
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Chapter Four I/O Redirection1 System Programming Shell Operators.
Sys Prog & Scrip - Heriot Watt Univ 1 Systems Programming & Scripting Lecture 12: Introduction to Scripting & Regular Expressions.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
2004/12/051/27 SPARCS 04 Seminar Regular Expression By 박강현 (lightspd)
Unix Programming Environment Part 3-4 Regular Expression and Pattern Matching Prepared by Xu Zhenya( Draft – Xu Zhenya(
Regular Expressions CS 2204 Class meeting 6 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
Adv. UNIX: REs/31 Advanced UNIX v Objectives –explain how to write Regular Expressions (REs) in vi and grep Special Topics in Comp. Eng.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CIRC Summer School 2016 Baowei Liu
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
Regular Expressions Copyright Doug Maxwell (
Lesson 5-Exploring Utilities
Looking for Patterns - Finding them with Regular Expressions
CIRC Summer School 2017 Baowei Liu
CST8177 sed The Stream Editor.
CIRC Winter Boot Camp 2017 Baowei Liu
Linux command line basics III: piping commands for text processing
Lecture 9 Shell Programming – Command substitution
Unix Scripting Session 4 March 27, 2008.
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
Unix Talk #2 (sed).
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CSCI The UNIX System Regular Expressions
1.5 Regular Expressions (REs)
CSC 4630 Meeting 4 January 29, 2007.
Presentation transcript:

I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright

(C) Doug Bowman, Virginia Tech, Redirecting stdout Instead of writing to the terminal, you can tell a program to print its output to another file using the > operator >> operator is used to append to a file Examples: man ls > ls_help.txt Echo $PWD > current_directory cat file1 >> file2

(C) Doug Bowman, Virginia Tech, Redirecting stdin Instead of reading from the terminal, you can tell a program to read from another file using the < operator Examples: Mail < message interactive_program < command_list

(C) Doug Bowman, Virginia Tech, Pipes and filters Pipe: a way to send the output of one command to the input of another Filter: a program that takes input and transforms it in some way wc - gives a count of words/lines/chars grep - searches for lines with a given string more sort - sorts lines alphabetically or numerically

(C) Doug Bowman, Virginia Tech, Examples of filtering ls -la | more cat file | wc man ksh | grep “history” ls -l | grep “gifford” | wc who | sort > current_users

(C) Doug Bowman, Virginia Tech, What is a regular expression (RE)? A pattern Defines a set of strings of characters Any string in the set is said to be “matched” by the RE (the RE matches the string)

(C) Doug Bowman, Virginia Tech, Why REs? Pattern matching is a useful tool in many real- world situations: Search for a file on a filesystem Find and replace strings in a file Extract particular data elements from a database REs are an important part of formal languages - one of the basic CS theory disciplines

(C) Doug Bowman, Virginia Tech, UNIX programs that use REs grep (search within files) egrep ( grep but with extended RE’s) vi/emacs (text editors) ex (line editor) sed (stream editor) awk (pattern scanning language) perl (scripting language)

(C) Doug Bowman, Virginia Tech, Characters vs. metacharacters In patterns, characters can be any character except a newline Metacharacters are special characters that have a special meaning To use metacharacters as regular characters in a pattern, quote them with the ‘\’ character

(C) Doug Bowman, Virginia Tech, Basic vs. Extended RE’s In basic regular expressions the metacharacters ?, +, {, |, (, and ) have no special meaning ( grep). To make them special, instead use the backslashed versions \?, \+, \{, \|, \(, and \) For extended regular expressions these are interpreted as special Grep –E  egrep

(C) Doug Bowman, Virginia Tech, Using egrep egrep pattern filename(s) To be safe, put quotes around your pattern Examples: egrep “abc” file.txt (print lines containing “abc”) egrep -i “abc” file.txt (same, but ignore case) egrep -v “abc” file.txt (print lines not containing “abc”) egrep -n “abc” file.txt (include line numbers)

(C) Doug Bowman, Virginia Tech, Metacharacters 1 Period (.): matches any single character “a.c” matches abc, adc, a&c, a;c, … “u..x” matches unix, uvax, u3(x, … Asterisk (*) matches zero or more occurrences of the previous RE Not the same as wildcards in the shell! “ab*c” matches ac, abc, abbc, abbbc, … “.*” matches any string

(C) Doug Bowman, Virginia Tech, Metacharacters 2 Plus (+): matches one or more occurrences of the preceding RE “ab+c” matches abc, abbc, but not ac Question mark (?): matches zero or one occurrences of the preceding RE “ab?c” matches ac, abc but not abbc Logical or (|): matches RE before or RE after bar “abc|def” matches abc or def

(C) Doug Bowman, Virginia Tech, Metacharacters 3 Caret (^): means beginning of line “^D” matches all strings starting with D Dollar sign ($) means end of line “d$” matches all strings ending with d Backslash (\): used to quote other metacharacters “file\.txt” matches file.txt but not fileatxt

(C) Doug Bowman, Virginia Tech, Metacharacters 4 Square brackets ([ ]) indicate a set/range of characters Any characters in the set will match ^ before the set means “not” - between characters indicates a range Examples: “[fF]un” matches fun, Fun “b[aeiou]g” matches bag, beg, big, bog, bug “[A-Z].*” matches any string beginning with a captial letter “[^abc].*” matches any string not starting with a, b, or c

(C) Doug Bowman, Virginia Tech, Metacharacters 5 Parentheses ( ): used to group REs when using other metacharacters “a(bc)*” matches a, abc, abcbc, abcbcbc, … “(foot|base)ball” matches football, baseball Braces ({ }): specify the number of repetitions of an RE “[a-z]{3}” matches three lowercase letters “m.{2,4}” matches strings starting with m between three and five letters

(C) Doug Bowman, Virginia Tech, What do these mean? egrep “^B.*s$” file egrep “ [0-9]{3} ” file egrep “num(ber)? [0-9]+” file egrep “word” file | wc -l egrep “[A-Z].*\?” file What about if they were grep?

(C) Doug Bowman, Virginia Tech, Practice Construct egrep commands that find in file : Lines beginning with a word of at least 10 characters Lines containing a student ID number in standard 3- part form Number of lines with 2 consecutive capitalized words Number of lines not ending in an alphabetic character Lines containing a word beginning with a vowel at the end of a sentence