CSC 352– Unix Programming, Spring 2015 April 28 A few final commands.

Slides:



Advertisements
Similar presentations
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Advertisements

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.
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Regular Expressions. u A regular expression is a pattern which matches some regular (predictable) text. u Regular expressions are used in many Unix utilities.
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.
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
System Programming Regular Expressions Regular Expressions
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
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)
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
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.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
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.
Appendix A: Regular Expressions It’s All Greek to Me.
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
©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.
Unix Programming Environment Part 3-4 Regular Expression and Pattern Matching Prepared by Xu Zhenya( Draft – Xu Zhenya(
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.
UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
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.
Class Introduction. Agenda Syllabus Topics Text etc.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
CIRC Summer School 2016 Baowei Liu
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
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
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
CSC 352– Unix Programming, Fall 2012
The Linux Command Line Chapter 7
CSC 352– Unix Programming, Spring 2016
LING 408/508: Computational Techniques for Linguists
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
Unix Talk #2 (sed).
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Review.
Presentation transcript:

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

Based on student 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 ' * -2.0' | bc [:-) ~] bc << EOF * -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=' * -2.0' [:-) ~] echo $expression bignum.py... FILES – Glob matching captures the * [:-) ~] expression=' \* -2.0' [:-) ~] echo $expression \* -2. – The backslash is now part of the string.

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

“No spaces” works without -f [:-) ~] expression='1.1+20*-2.0' [:-) ~] echo $expression *-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 ashref.html#Conditional-Constructs ashref.html#Conditional-Constructs [:-) ~] [[ a -lt 4 && a -gt 2 ]] [:-) ~] echo $? 0 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 $? 0 [:-) ~] [[ 1. =~ $number ]] ; echo $? 0 [:-) ~] [[ 1.1 =~ $number ]] ; echo $? 0 [:-) ~] [[.1 =~ $number ]] ; echo $? 0 [:-) ~] [[ 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 | P2is 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 abc ABCDEFGHIJKLM AaAaBbVv [:-) ~/unix] egrep '[0-9]+' re1.txt abc

Regular expression examples p.2 [:-) ~/unix] egrep '^[0-9]+$' re1.txt [:-) ~/unix] egrep '^[0-9]*$' re1.txt [:-) ~/unix] egrep '[0-9]*' re1.txt abcdefg abc ABCDEFGHIJKLM AaAaBbVv

Regular expression examples p.3 [:-) ~/unix] egrep '[0-9]+.?[0-9]*' re1.txt abc [:-) ~/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 123abc

Regular expression examples p.4 [:-) ~/unix] egrep '(([0-9]+)|([A-Za-z]+))$' re1.txt abcdefg abc ABCDEFGHIJKLM AaAaBbVv [:-) ~/unix] egrep '[^A-Za-z0-9]+$' re1.txt

Regular expression examples p.5 [:-) ~/unix] egrep '[^A-Za-z0-9]?$' re1.txt abcdefg abc ABCDEFGHIJKLM AaAaBbVv [:-) ~/unix] egrep '[^A-Za-z0-9]*$' re1.txt abcdefg abc ABCDEFGHIJKLM AaAaBbVv

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 abc 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 abc 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 MATCH 123abc