CSC 352– Unix Programming, Spring 2016

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
EMT 2390L Lecture 4 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
2006-Jan-231 Shell Scripts Jacob Morzinski
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Shell Script Examples.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
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.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
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.
CSC 352– Unix Programming, Spring 2015 April 28 A few final commands.
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.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
Regular Expression - Intro Patterns that define a set of strings (or, pieces of a string) Not wildcards (similar notion, but different thing) Used by utilities.
Shell Advanced Features. Module 8 Shell Advanced Features ♦ Introduction In Linux systems, the shells are often referred to as command line interfaces.
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
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
By Corey Stokes 9/14/10. What is grep? Global Regular Expression Print grep is a command line search utility in Unix Try: Search for a word in a.cpp file.
BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 5 – Regular Expressions, grep, Other Utilities.
Adv. UNIX: REs/31 Advanced UNIX v Objectives –explain how to write Regular Expressions (REs) in vi and grep Special Topics in Comp. Eng.
CSC 352– Unix Programming, Fall 2011 November 8, 2011, Week 11, a useful subset of regular expressions, grep and sed, parts of Chapter 11.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
CIRC Summer School 2016 Baowei Liu
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
Regular Expressions Copyright Doug Maxwell (
CSC 352– Unix Programming, Spring 2016
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CIRC Summer School 2017 Baowei Liu
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 594 Topics in AI – Natural Language Processing
Regular Expressions in Perl
CIRC Winter Boot Camp 2017 Baowei Liu
CSC 352– Unix Programming, Fall 2012
CIRC Summer School 2017 Baowei Liu
Regular Expression - Intro
Lecture 9 Shell Programming – Command substitution
Unix Scripting Session 4 March 27, 2008.
CSC 352– Unix Programming, Fall 2012
CSC 594 Topics in AI – Natural Language Processing
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
The Linux Command Line Chapter 7
LING 408/508: Computational Techniques for Linguists
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
Unix Talk #2 (sed).
Essential Shell Programming
CSCI The UNIX System Regular Expressions
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Regular Expression in Java 101
Introduction to Bash Programming, part 3
Review.
Presentation transcript:

CSC 352– Unix Programming, Spring 2016 April 2016 A few final commands

Based on 2015 assn 3 solutions These are some bash shell mechanisms and Unix utilities used by students in assignment 3 that can be very useful. I had forgotten about some of these. Some may be buried in or missing from the textbook. I always learn or re-learn when teaching Unix.

bc – the “bench calculator” It reads standard input, not command line. Following POSIX bc operators behave exactly like their C counterparts: + - * / += -= *= /= ++ -- < > == != <= >= ( ) [ ] { } GNU bc has others, but they are not portable.

Some bc examples [:-) ~] echo '1.1 + 20 * -2.0' | bc -38.9 [:-) ~] bc << EOF 1.1 + 20 * -2.0 EOF Latter is a “here document” for supplying literal text to standard input. Note that 0 means false and non-0 means true in a bc expression, just like C & C++, but opposite of shell exit status.

Some pitfalls in = assignment [:-) ~] expression='1.1 + 20 * -2.0' [:-) ~] echo $expression 1.1 + 20 bignum.py ... FILES ... -2.0 Glob matching captures the * [:-) ~] expression='1.1 + 20 \* -2.0' 1.1 + 20 \* -2. The backslash is now part of the string.

More pitfalls in = assignment expression="1.1 + 20 * -2.0” glob matches expression="1.1 + 20 \* -2.0” captures \ [:-) ~] expression=1.1 + 20 \* -2.0 -bash: +: command not found [:-) ~] set –f # SHUT OFF GLOBBING [:-) ~] ls * ls: cannot access *: No such file or directory [:-) ~] expression='1.1 + 20 * -2.0' [:-) ~] echo $expression 1.1 + 20 * -2.0 [:-) ~] echo $expression | bc -38.9

“No spaces” works without -f [:-) ~] expression='1.1+20*-2.0' [:-) ~] echo $expression 1.1+20*-2.0 [:-) ~] echo $expression | bc -38.9

[[ EXPRESSION ]] [[ expression ]] Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below under CONDITIONAL EXPRESSIONS. Word splitting and pathname expansion are not performed on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed. Conditional operators such as -f must be unquoted to be recognized as primaries.

[[ … ]] examples http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs [:-) ~] [[ a -lt 4 && a -gt 2 ]] [:-) ~] echo $? You can use logical connectives && and || inside the test brackets.

[[ … ]] regular expression matching [:-) ~] number='^(([0-9]+(\.[0-9]*)?)|([0-9]*\.[0-9]+))$’ The \. Is necessary to match literal “.” -- . by itself matches any char. [:-) ~] [[ 1 =~ $number ]] ; echo $? [:-) ~] [[ 1. =~ $number ]] ; echo $? [:-) ~] [[ 1.1 =~ $number ]] ; echo $? [:-) ~] [[ .1 =~ $number ]] ; echo $? [:-) ~] [[ 1f =~ $number ]] ; echo $? 1

egrep (extended grep) patterns pattern * 0 or more occurrences of previous character or (expression) pattern + 1 or more occurrences of previous character or (expression) pattern ? 0 or 1 occurrence of previous character or (expression) pattern . any single character pattern [pqr] any single character from set p, q or r pattern [a-zA-Z] any single character from range a-z or A-Z pattern [^pqr] any single character *not* from set p, q or r pattern ( EXPR ) is for subexpression grouping pattern P1 | P2 is for pattern P1 or P2 pattern ^ the start of the string being searched for a match pattern $ the end of the string being searched for a match pattern \ escapes the next character so it is treated as a regular char These are the most useful regular expression patterns available to grep, sed, [[ $string =~ $pattern ]] tests, and for searching in emacs and vi. The shell uses so-called “glob-style matching” for strings (*.java), which differ from regular expressions (.*\.java) used by grep, sed, emacs and vi.

Regular expression examples p.1 [:-) ~/unix] cat re1.txt abcdefg 1234567 123.456 123abc !@#$%^&*() ABCDEFGHIJKLM AaAaBbVv [:-) ~/unix] egrep '[0-9]+' re1.txt

Regular expression examples p.2 [:-) ~/unix] egrep '^[0-9]+$' re1.txt 1234567 [:-) ~/unix] egrep '^[0-9]*$' re1.txt [:-) ~/unix] egrep '[0-9]*' re1.txt abcdefg 123.456 123abc !@#$%^&*() ABCDEFGHIJKLM AaAaBbVv

Regular expression examples p.3 [:-) ~/unix] egrep '[0-9]+.?[0-9]*' re1.txt 1234567 123.456 123abc [:-) ~/unix] egrep '^[0-9]+.?[0-9]*$' re1.txt [:-) ~/unix] egrep '^[0-9]+[^0-9][0-9]*$' re1.txt [:-) ~/unix] egrep '^[0-9]+[^0-9]?[0-9]*$' re1.txt [:-) ~/unix] egrep '^[0-9]+[^0-9]?[A-Fabcdef]+$' re1.txt

Regular expression examples p.4 [:-) ~/unix] egrep '(([0-9]+)|([A-Za-z]+))$' re1.txt abcdefg 1234567 123.456 123abc ABCDEFGHIJKLM AaAaBbVv [:-) ~/unix] egrep '[^A-Za-z0-9]+$' re1.txt !@#$%^&*()

Regular expression examples p.5 [:-) ~/unix] egrep '[^A-Za-z0-9]?$' re1.txt abcdefg 1234567 123.456 123abc !@#$%^&*() ABCDEFGHIJKLM AaAaBbVv [:-) ~/unix] egrep '[^A-Za-z0-9]*$' re1.txt

sed writes to standard output, sed –r uses extended Res p.1 [:-) ~/unix] egrep '[^A-Za-z0-9]+$' re1.txt !@#$%^&*() [:-) ~/unix] sed -e 's/[^A-Za-z0-9]+$/AllFixedUp/g' re1.txt abcdefg 1234567 123.456 123abc ABCDEFGHIJKLM AaAaBbVv

sed writes to standard output, sed –r uses extended Res p.1 [:-) ~/unix] sed -r -e 's/[^A-Za-z0-9]+$/AllFixedUp/g' re1.txt abcdefg 1234567 123.456 123abc AllFixedUp ABCDEFGHIJKLM AaAaBbVv sed does NOT modify the source file. It sends to stdout but you could: sed -r -e 's/[^A-Za-z0-9]+$/AllFixedUp/g' re1.txt > tmpfile.txt mv tmpfile.txt re1.txt

[[ =~ ]] pattern matching [:-) ~/unix] pattern='^[0-9]+.?[a-z]*$' [:-) ~/unix] echo $pattern ^[0-9]+.?[a-z]*$ [:-) ~/unix] for line in `cat re1.txt` > do > if [[ $line =~ $pattern ]] # [[ ! $line =~ $pattern ]] for non-matching lines > then > echo "MATCH $line" > fi > done MATCH 1234567 MATCH 123abc