Grep and Shell Programming

Similar documents
Vi & Shell Scripting

COMP 4/6262: Programming UNIX

Shell Programming (bash)

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

bash Execution Control COMP2101 Winter 2019

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

UNIX shell scripting

Shells & Shell Programming (Part B)

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

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

Bourne Shell Reference

CS Unix Tools & Scripting

A shell can be used in one of two ways:

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

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

9.2 Linux Essentials Exam Objectives

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Shells and Shell Programming

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Bash scripting basics

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Assignment clarifications

Basic Linux (Bash) Commands

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

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

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Shells and Shell Programming

Essentials for Scientific Computing: Bash Shell Scripting Day 3

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

Review of Fundamentals

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Linux shell programming for Raspberry Pi Users - 2

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

5/20/2007. Touring Essential Programs

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

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Useful Unix Commands Cheat Sheet

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan

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

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Essential Skills for Bioinformatics: Unix/Linux

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

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

Using echo command in shell script

5/8/2012. Exploring Utilities Chapter 5

Shell programming. Introduction to Operating Systems

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

Introduction to UNIX Part II

Course Outline. TERM EFFECTIVE: Fall 2016 CURRICULUM APPROVAL DATE: 11/23/2015

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

Програмиранев UNIX среда

CS1101: Lecture 9 The Shell as a Programming Language

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Regular expressions: Text editing and Advanced manipulation. HORT Lecture 4 Instructor: Kranthi Varala

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

CSE 374 Midterm Exam Sample Solution 2/11/13. Question 1. (8 points) What does each of the following bash commands do?

Linux Shell Script. J. K. Mandal

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

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

LING 408/508: Computational Techniques for Linguists. Lecture 5

Wildcards and Regular Expressions

Last Time. on the website

Perl. Perl. Perl. Which Perl

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

Introduction: What is Unix?

Linux shell & shell scripting - II

Linux Fundamentals (L-120)

Shell Programming Systems Skills in C and Unix

Lecture 8. Introduction to Shell Programming. COP 3353 Introduction to UNIX

I/O and Shell Scripting

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2

CSC 2500: Unix Lab Fall 2016

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

Open up a terminal, make sure you are in your home directory, and run the command.

Lecture # 2 Introduction to UNIX (Part 2)

Shell Control Structures

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

LINUX FUNDAMENTALS (5 Day)

C Shell Tutorial. Section 1

CSCI2467: Systems Programming Concepts

Perl. Many of these conflict with design principles of languages for teaching.

Assignment 3, Due October 4

/smlcodes /smlcodes /smlcodes. Shell Scripting TUTORIAL. Small Codes. Programming Simplified. A SmlCodes.Com Small presentation

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

CSCI-GA Scripting Languages

Scripting. More Shell Scripts Loops. Adapted from Practical Unix and Programming Hunter College

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CSCI 2132 Software Development. Lecture 3: Unix Shells and Other Basic Concepts

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

Mills HPC Tutorial Series. Linux Basics II

Introduction to Linux

Chapter 9. Shell and Kernel

Transcription:

Grep and Shell Programming Comp-206 : Introduction to Software Systems Lecture 7 Alexandre Denault Computer Science McGill University Fall 2006

Teacher's Assistants Michael Hawker Monday, 14h30 to 16h30 McConnell, Room 322 Robert Kaplow Wednesday, 9:30 to 11:30 T.A. room, Trottier, 3 rd floor Jun Wang Friday, 14h00 to 16h00 T.A. room, Trottier, 3 rd floor

Editor Command line text editors allow you to create/edit files at the command line. Several text editors are available. vi is one of the original text editor available on Unix. It's very difficult to use and learn. However, its very powerful and available on every Unix machines. pico is a simple text editor based on the pine mail client. It's very easy to use, and is available on most Unix machines. emacs is a very popular and powerful. Considering the number of features it has, it should be considered a heavy weight client. You can also use graphical text editors, such as bluefish, gedit or jedit. As a long term investment, I highly suggest you learn vi.

Example of Text Editors Text based, console Vi Emacs Pico Ed JStar / Jove Edit (dos) Graphic based, GUI Xemacs Bluefish Gedit (Gnome) Kate (KDE) Jedit (java) Notepad (windows)

Regular Expressions Several Unix commands and editors allow you to search on text patterns. These text patterns are known as regular expressions (regex). Examples of regular expressions include: Text starting with the letter a and finishing with the letter z. Text with at least one number, but not starting with the letter a or b. Text with a letter repeated three times in a row. Text contains the string abc exactly three times.

Regex Syntax Take a look at the Regex Syntax quick sheet. Literal characters are combination to represent special characters. Character classes are combination to represent groups of characters. Repetition indicate how often a character should be appear to be a match. Anchors determine where the matching string must be found.

grep, sed and awk grep [options] string file search for occurrences of the string. sed [options] file stream editor for editing files. awk [options] file scan for patterns in a file and process the results.

Grep grep is used to search for the occurrence of a regular expression in files. Regular expressions, are best specified in apostrophes (or single quotes) when use with grep. Some common options include: -i : ignore case -c : report only a count of the number of lines containing matches -v : invert the search, displaying only lines that do not match -n : display the line number along with the line on which a match was found -l : list filenames, but not lines, in which matches were found

Examples of grep Consider the following text file : Alex Marc Micheal Ting Juan Jeremy Jessica Yannick Nicolas Jean-Sebastien Nadeem

Examples of grep (cont.) Grep for a specific string... [adenau][rogue][~/cs206] grep Je demo.txt Jeremy Jessica Jean-Sebastien [adenau][rogue][~/cs206] grep -n Je demo.txt 6:Jeremy 7:Jessica 10:Jean-Sebastien [adenau][rogue][~/cs206] grep -c Je demo.txt 3

Examples of grep (cont.) Grep for vowels... [adenau][rogue][~/cs206] grep -i ^[aeiouy] demo.txt Alex Yannick [adenau][rogue][~/cs206] grep -i [aeiouy]$ demo.txt Jeremy Jessica [adenau][rogue][~/cs206] grep -i [aeiouy]\{2\} demo.txt Micheal Juan Yannick Jean-Sebastien Nadeem

Examples of grep (cont.) Grep for specific characters... [adenau][rogue][~/cs206] grep -i ^.e demo.txt Jeremy Jessica Jean-Sebastien [adenau][rogue][~/cs206] grep -i ^.e\ a.$ demo.txt Micheal Juan Jeremy Jessica Nicolas Jean-Sebastien

When to use grep Grep is a useful tool to find specific strings. Outlining all the errors in a log file. Finding a specific string in a collection of source files. It becomes an even more powerful tool when combined to other utilities. [adenau][rogue][~/cs206] ps -e grep java 14256 pts/1 00:18:30 java 21395? 00:00:08 java 11218 pts/4 00:03:51 java

Shell Scripting A shell programs (or script) containing a series of shell commands. The first line of the script should start with #! which indicates to the kernel that the script is directly executable. You immediately follow this with the name of the shell, or program (spaces are allowed), to execute, using the full path name. Different languages can be use to script (sh, bash, perl, python, ruby, etc). To set up a Bourne shell script the first line would be: #! /bin/sh You also need to specify that the script is executable by setting the proper permissions on the file. % chmod +x shell_script

Variables There are three kinds of variables in a shell script: Environment Variable : these variables are used to customize the operating system and the shell to your needs. User-created : these variables are created by the script itself. Positional Parameters : these variables store the parameter used to start the script.

Positional Variables $# : number of arguments on the command line $- : options supplied to the shell $? : exit value of the last command executed $$ : process number of the current process $! : process number of the last command done in background $n : argument on the command line, where n is from 1 through 9, reading left to right $0 : the name of the current shell or program $* : all arguments on the command line ( $1 $2... $9 ) $@ : all arguments on the command line, each separately quoted ( $1 $2... $9 )

Your first Unix Script The following script will print out the positional variables: #!/bin/sh echo "$#:" $# echo $-: $- echo $?: $? echo $$: $$ echo $!: $! echo $3: $3 echo $0: $0 echo $*: $* echo $@: $@

Shell Scripts A shell script runs from top to bottom. If statements and loop can be used to alter the control flow. You can also create functions. The # character is usually used to denote a comment. The #! at the start of the script indicates which program should execute/interpret the script. Unlike other programming languages, scripts are sometime sensitive to extra spaces.

Simple Script The following script gathers information about the system and stores it in a file specified at the command line. #!/bin/sh uname -a > $1 date >> $1 who >> $1 The output was as follows : [adenau][rogue][~/cs206]./info.sh output.txt [adenau][rogue][~/cs206] cat output.txt Linux rogue 2.6.12-gentoo-r4 #1 SMP... Thu Aug 10 10:57:38 EDT 2006 adenau pts/0 Aug 10 08:04 (dz2.cs.mcgill.ca)

Reading from STDIN The read command allows you to read a string from STDIN. That string is then stored in the specified variable. #!/bin/sh echo "What is your name?" read name echo "Your name is $name."

Arithmetic Operations The shell was never designed for numerical work. To do mathematical (integer) operations, you can to use the expr command. The following example script adds two numbers passed at the command line and outputs the answer to STDOUT. #!/bin/sh sum= expr $1 + $2 echo $sum

test Before discussing control statements (if, for, etc), we need to check out the test command. The test command is used to evaluate an expression, or in our case, a condition. Although shells do contain operators to test a condition, they are not as versatile and universal as test. The test command can evaluate condition at the file, string or integer level.

File Tests -r : true if it exists and is readable -w : true if it exists and is writable -x : true if it exists and is executable -f : true if it exists and is a regular file -d : true if it exists and is a directory -h or -L : true if it exists and is a symbolic link and many more...

String Tests -z string : true if the string length is zero -n string : true if the string length is non-zero string1 = string2 : true if string1 is identical to string2 string1!= string2 : true if string1 is non identical to string2 string : true if string is not NULL

Integer Tests n1 -eq n2 : true if integers n1 and n2 are equal n1 -ne n2 : true if integers n1 and n2 are not equal n1 -gt n2 : true if integer n1 is greater than integer n2 n1 -ge n2 : true if integer n1 is greater than or equal to integer n2 n1 -lt n2 : true if integer n1 is less than integer n2 n1 -le n2 : true if integer n1 is less than or equal to integer n2

Logical Operators for Tests! : negation (unary) -a : and (binary) -o : or (binary) () : expressions within the () are grouped together. You may need to quote the () to prevent the shell from interpreting them.