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

Similar documents
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

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

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

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

UNIX files searching, and other interrogation techniques

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

CST8207: GNU/Linux Operating Systems I Lab Ten Boot Process and GRUB. Boot Process and GRUB

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Paolo Santinelli Sistemi e Reti. Regular expressions. Regular expressions aim to facilitate the solution of text manipulation problems

Principles of Bioinformatics. BIO540/STA569/CSI660 Fall 2010

Module 8 Pipes, Redirection and REGEX

Common File System Commands

commands exercises Linux System Administration and IP Services AfNOG 2015 Linux Commands # Notes

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

5/20/2007. Touring Essential Programs

Multiple Choice - 58 Questions - 10 of 15%

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

This is Lab Worksheet 3 - not an Assignment

Systems Programming/ C and UNIX

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

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

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

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

5/8/2012. Exploring Utilities Chapter 5

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

Introduction to UNIX Part II

Wildcards and Regular Expressions

This is Lab Worksheet 13 - not an Assignment. Boot Process and GRUB

UNIX System Programming Lecture 3: BASH Programming

ITST Searching, Extracting & Archiving Data

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

Introduction to the UNIX command line

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

ADVANCED LINUX SYSTEM ADMINISTRATION

C Shell Tutorial. Section 1

More regular expressions, synchronizing data, comparing files

2) clear :- It clears the terminal screen. Syntax :- clear

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Getting your department account

Consider the following program.

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

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

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

9.2 Linux Essentials Exam Objectives

Chapter-3. Introduction to Unix: Fundamental Commands

Lecture 18 Regular Expressions

bash, part 3 Chris GauthierDickey

Basics. I think that the later is better.

Practical 02. Bash & shell scripting

More Scripting Todd Kelley CST8207 Todd Kelley 1

Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

CS160A EXERCISES-FILTERS2 Boyd

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

do shell script in AppleScript

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

Course 144 Supplementary Materials. UNIX Fundamentals

File Commands. Objectives


Certification. String Processing with Regular Expressions

Bioinformatics? Reads, assembly, annotation, comparative genomics and a bit of phylogeny.

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

- c list The list specifies character positions.

UNIX Shell Programming

UNIX / LINUX - REGULAR EXPRESSIONS WITH SED

The e switch allows Perl to execute Perl statements at the command line instead of from a script.


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

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

Lab 4: Shell Scripting

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

Please note that CNE 216 is a brand new course that has never been taught on the George campus; it will be taught for the first time in the fall of

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

Awk & Regular Expressions

Mineração de Dados Aplicada

User Commands sed ( 1 )

Week 5 Lesson 5 02/28/18

CSCI 2132 Software Development. Lecture 4: Files and Directories

Bash Programming. Student Workbook

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Linux shell scripting Getting started *

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

S E C T I O N O V E R V I E W

A shell can be used in one of two ways:

Essentials for Scientific Computing: Stream editing with sed and awk

Subcontractors. bc math help for the shell. interactive or programatic can accept its commands from stdin can accept an entire bc program s worth

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

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

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

: the User (owner) for this file (your cruzid, when you do it) Position: directory flag. read Group.

5/8/2012. Creating and Changing Directories Chapter 7

COMS 6100 Class Notes 3

Getting to grips with Unix and the Linux family

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

Session: Shell Programming Topic: Advanced Commands

A Brief Introduction to the Linux Shell for Data Science

Transcription:

CST8177 - Lab #5 Student Name: Student Number: Lab section: Working with Regular Expressions (aka regex or RE) In-Lab Demo - List all the non-user accounts in /etc/passwd that use /sbin as their home directory. State the purpose of each field in a password file entry - see passwd(5). Overview Regular expressions are used for pattern matching. Regular expressions are interpreted by specific utilities, such as grep, and not by the shell. To prevent the shell from interpreting special characters, since some are the same ones the shell uses, use quotes when passing a regular expression as an argument. Examples: grep ro*t /etc/passwd grep 'ro*t' /etc/passwd Regex metacharacters are different from file glob (wild card) metacharacters (although some, notably *, are the same character). grep stands for global regular expression and print, derived from the Unix text editor ed construct g/re/p. It will always match the FIRST and LONGEST string. Summary of regexes of the basic set Meaning. Matches any single character (except newline, 0x0A). Example: ro.t matches root, robt, ro3t, ro@t, and so on Note: The newline is not considered a printable character. * Matches zero or more of the preceding item (unlike in a file glob, it cannot stand alone; it always modifies the previous item) Example: the pattern ro*t matches rt, rot, root, rooot and so on for any number of o (but no other letter). [...] Matches any single character in the list (like file glob). Example: l[io]ve matches live or love but not lave or lrve Note: Ranges like a-z or 0-9 are valid as long as the start is lower in the ASCII list than the end ([0-2] is OK, [2-0] is not). Use LC_ALL=C. To use the range indicator - as a match character, escape it as \-. [^...] Matches any character not in the list. Note: If a caret (^) is in a [...] list but not at the beginning, it is interpreted as being just a normal character. It can also be escaped by \. \(...\) Group into an item. Used with \, select one item from a list \{n,m\} Match the preceding item at least '\{n\}' or more times; or exactly '\{n,\}' times; or using \{n,n\}, from n to m times.

^ Anchors the regex at the beginning of the line if the caret is the first regex character. Example: These will provide different output: grep 'root' /etc/passwd grep '^root' /etc/passwd $ Anchors the regex at the end of the line if the dollar sign is the last regex character. Example: These will provide different output: grep 'root' /etc/passwd grep 'root$' /etc/passwd '^$' The regex to represent an empty line. Exercise #1: Viewing regular expression output Type the following 7 lines of text exactly in vi as the file lab4-re using the linebreaks given as [Enter] only (or copy/paste from the document, replacing [ENTER] and [TAB], and ensuring that exactly 7 lines result): How to Please your Technical Support Department[Enter] Tip:[Enter] When you call us to have your computer moved, leave it buried under postcards and family pictures.[enter] We don't have a life and we are deeply moved when catching a glimpse of yours.[enter] [Enter] Thank you![enter] [Tab]Your IT Department (Call 555)[Enter] Type the following commands (omit the comment - # and following), and record the line numbers 1 to 7 only, to observe the result of the commands. Note: The -n switch of grep displays the line number in addition to the line found, if any. Example: grep -n '^root:' /etc/passwd # also try with another user id grep -n '.' lab4-re # matches any line with any single char anywhere grep -n '\.' lab4-re # matches any line with a (literal) period grep -n 'T' lab4-re # matches any line with the character T grep -n '^T' lab4-re # matches any line beginning with the char T grep -n '^[A-Z]...$' lab4-re # Match 4-letter line starting upper case grep -n '^[A-Z][a-z]*:' lab4-re # Matches any alpha line with a colon

grep -n '^$' lab4-re # Matches any empty line grep -n '[Ii][Tt]' lab4-re # Matches any line with IT, it, It, it grep -n -i 'it' lab4-re # Also matches as above grep -n '[0-9]' lab4-re # matches any line containing a number grep -n 'call' lab4-re # matches any line with the string grep -n 'ca.*l' lab4-re # matches 0 or more char between 'ca' and 'l' grep -n 'cal*' lab4-re # matches 'ca' followed by 0 or more 'l's What is the difference between the last 2 regexes: They both use c, a, *, and l? Exercise #2: Searching a system file using grep Use grep to search the password file for specific strings using regular expressions. As root, make a backup copy of your /etc/passwd file and create an account for each of the following users: afoo, foo, foobar. Read the information in man 5 passwd for details of the password file and its colon-separated fields, and man 5 shadow for the shadow password file. Hint: Anchor your regex on something solid, like the start or end of the line, or on the colon-separators, or both. Record the regex and the output for each of the following actions: Display root's account (only one line of output) _ Display foo's account (only one line of output) _ Display foobar's account (only one line of output) _ Display all accounts with /sbin/nologin as the shell (7 th and last field) - list the userids _

Display all accounts with /home as the parent home directory (6 th field) - list the userids _Search all accounts in the password or shadow file that have no valid password - list the userids; which file? _ Search all accounts in the password or shadow file that have a locked password - list the userids; which file? _ Exercise #3: Extended REs Some examples using the extended regular expression set: ORing To work with the extended regular expression set, use egrep instead of grep. The pipe symbol is the regex OR operator and allows you to look for more than one pattern, in the form (pattern-1 pattern-2... pattern-n). This OR is the inclusive or, and results in true if this or that or both are true. That is, if you evaluate a b logically, when either a is true or b is true or both are true, the result is true. Example: egrep '^(root bin):' /etc/passwd Compare the example above with egrep '(root bin):' /etc/passwd. If the results are different, why is this so? _ Display all accounts with group id of 100 or 500: egrep "^[^:]*:[^:]*:[^:]*: (100 500):" /etc/passwd cut -d : -f 1 _ Why or how does this regex work? _ Display all accounts with group id 0 to 100 (that is, a 1-digit number, or a 2-digit number, or a 3-digit number starting with the digit '1'): egrep "^[^:]*:[^:]*:[^:]*:([0-9] [0-9][0-9] 100):[^:]*:[^:]*:[^:]*$" /etc/passwd cut -d : -f 1 Try this again with egrep "^[^:]*:[^:]*:[^:]*:([0-9] [0-9][0-9] 100):" /etc/passwd cut -d : -f 1 Why or how does each regex work? _

Working with some grep options The grep utility has a number of options. Some of the most frequently used (there are lots more) include: -c displays a count of matching lines -i ignores the case or letters in making comparisons -n displays line number -q quiet: used when scripts collect the exit status $? as a POSIX alternative to redirecting output to /dev/null -v inverts the search to display only lines that do NOT match -w matches the string as a word Experiment with the grep options above in addition to these samples. grep -c "^" lab4-re and grep -c "$" lab4-re How many lines are in the file lab4-re? Why or how do these regexes work? What happens if you omit the regex and use grep -c lab4-re grep -v "." lab4-re Why or how does this regex work? grep -v "\." lab4-re Why or how does this regex work? Using at least the -v option of grep, display only lines in lab4-re that do not contain the string "you". Show your grep command here: Count all lines with the string "you" and separately, list only their line numbers. Show your two grep commands here (you may need to pipe grep's output to another utility): Did any of your "you" matches surprise you? Which and why? (You may have to pretend to be easily surprised!)