agenda - seneca collegemurray.saul/bif713/... · use egrep or grep –e when using grep with...
TRANSCRIPT
![Page 1: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/1.jpg)
BIF713
Additional Utilities
![Page 2: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/2.jpg)
Linux Utilities
You have learned many Linux commands. Here are some more that you can use:
Data Manipulation (Reg Exps) Miscellaneous Printer Commands
![Page 3: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/3.jpg)
Data Manipulation
grep - print lines matching a pattern head - output the first part of files tail - output the last part of files sort - sort lines of text files diff - find differences between two files file - determine file type
![Page 4: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/4.jpg)
4
Regular Expressions
Definition:
A Regular Expression refers to a set of one or more strings of characters.
Regular expressions can be used with Linux / UNIX commands such as grep, egrep, man, more, less, vi.
![Page 5: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/5.jpg)
5
Regular Expressions
Simple Regular Expressions
A Simple Regular Expression is a considered to be a “simple” match using patterns involving all or portions of characters
Here are some examples:
grep –i “435” course_codes (Matches strings of text containing 435 such as “OPS435”)
grep –i “there” file.txt (Matches strings of text containing pattern “there”
such as “there”, “therefore”, “there they go again…”
![Page 6: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/6.jpg)
6
Regular Expressions
Complex Regular Expressions
A Complex Regular Expression uses special characters with letters and numbers in order to provide an ambiguous regular expression match.
This is similar to File Name Expansion, except we are matching strings (not filename), and the rules for searching symbols differ…
![Page 7: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/7.jpg)
7
Regular Expressions
Complex Regular Expression Symbols:
. Matches any character
* Matches one or more occurrences of that preceeding character
^ Anchors search to beginning of character string
(line)
$ Anchors search to end of character string (line)
[ .. ] Matches any single character that belongs in square brackets. For opposite, use the notation [^.. ]
![Page 8: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/8.jpg)
8
Regular Expressions Complex Regular Expressions - Examples
Explain what the following Linux commands using complex regular expressions will do:
grep “^the” file.txt
grep “the$” file.txt
grep “^the$” file.txt
grep “^[0-9]*” file.txt
grep “^[0-9]*$” file.txt
grep “^[0-9].*$” file.txt
![Page 9: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/9.jpg)
9
Regular Expressions Complex Regular Expressions – Examples / Continued…
Explain what the following Linux commands using complex regular expressions will do:
var=“I am hungry”; echo $var | grep “^[A-Z].*[a-z]” file.txt
var=“I am hungry”; echo $var | grep “^[a-z].*[a-z]” file.txt
var=“I like OPS435”; echo $var | grep “^[^0-9].*[0-9]” file.txt
var=“OPS435”; echo $var | grep “..” file.txt
var=“OPS435”; echo $var | grep “^..$” file.txt
var=“OPS435”; echo $var | grep “^……$” file.txt
var=“OPS435A”; echo $var | grep “^……$” file.txt
![Page 10: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/10.jpg)
10
Regular Expressions
Extended Regular Expressions
Symbols to include advanced (extended) features such as repetition, and grouping.
Use egrep or grep –E when using grep with extended regular expressions.
You can use complex and regular expressions together as long as you use grep command as egrep or grep –E…
![Page 11: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/11.jpg)
11
Regular Expressions Extended Regular Expression Symbols:
{1,} One or more occurrences of preceding character
{0,1} Zero or one occurrence of preceding character
{2,5} Between 2 and 5 occurrences of preceding character
(pattern){1,} One or more occurrences of “pattern”
NOTE: There are some short-cut symbols:
+ = {1,}
? = {0,1}
![Page 12: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/12.jpg)
12
Regular Expressions Extended Regular Expressions - Examples
Explain what the following Linux commands using extended regular expressions will do:
grep “^[+-]{0,1}[0-9]{1,}$” file.txt
egrep “^[+-]?[0-9]+$” file.txt
egrep “^[+-]?[0-9]?$” file.txt
grep -Ei “^(the){2,}” file.txt
![Page 13: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/13.jpg)
13
Regular Expressions
Other Utilities (vi):
Regular expressions can be used with the vi editor to make it easier to search for complex patterns that are contained in files.
The commands to search and replace regular expressions are typed in “last-line mode”
![Page 14: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/14.jpg)
14
Regular Expressions
Examples (using vi within “last-line” mode):
:s/pattern1/pattern2/ - substitutes pattern2 for the first occurrence of pattern1
:1,$ s/pattern1/pattern2/ - substitutes pattern2 for the first occurrence of pattern1 in each line of the file ($ means last line, . means current line)
:.,.+10 s/pattern1/pattern2/ - substitutes 11 lines starting with current line
:s/pattern1/ *** & ***/ - & is the value of the string matched
by pattern1, and is the only character with special meaning in the second pattern
![Page 15: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/15.jpg)
15
Regular Expressions
Other Utilities (man, more, less):
Regular expressions can be used with the man, more and less utilities to navigate to a location that matches a pattern
Examples:
/^The/ /ward$/ /^[0-9].*[0-9]$/ /therefore.*done/
![Page 16: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/16.jpg)
Miscellaneous
who – show who is logged in date – print or set the system date and time
which – show the full path of (shell) commands
finger – user information lookup program
mail – send and receive mail
![Page 17: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/17.jpg)
Print commands
lpr – print files lpq – show print queue status lprm – cancel print jobs
![Page 18: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/18.jpg)
grep
grep takes a pattern, read standard input or a list of files, and outputs the lines containing matches for the pattern.
Example:grep foo *
Print lines in any of the files in the current directory that contain the pattern “foo”.
![Page 19: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/19.jpg)
grep options
Major options for grep:
-ldisplay name of the file that has matching line
-r search all the files in the current directory and all its subdirectory for the given pattern
-n prefix each output with line number -w search for matching word
-v output lines that do not contain the given pattern
![Page 20: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/20.jpg)
grep examples
grep -r foo .
Print all the lines in all the files in the current directory and all its subdirectories that contains the pattern “foo”.
grep -lr foo .
Similar as above but only print the names of the files that contains the pattern “foo”
![Page 21: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/21.jpg)
grep examples
Print all the lines in the file “bar” that contains the pattern “foo”[uli@seneca misc]$ grep foo barThis line contains the word foo and bar.
Do you like to play football or basket ball?
Same as above but prefix each line of output with the line number within the file “bar”
[uli@seneca misc]$ grep -n foo bar 3:This line contains the word foo and bar.
4:Do you like to play football or basket ball?
![Page 22: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/22.jpg)
grep examples
Print all the lines in the file “bar” that contains the word “foo”[uli@seneca misc]$ grep -w foo barThis line contains the word foo and bar.
Print all the lines in the file “bar” that does not contain the pattern “foo”
[uli@seneca misc]$ grep -v foo bar The name of this file is called bar.
This file has only five line.
This is the end of the file.
![Page 23: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/23.jpg)
head & tail
head bar Display the first 10 line of the file “bar”
head -5 bar Display the first 5 lines of the file “bar”
tail bar Display the last 10 lines of the file “bar”
tail -5 bar Display the last 5 lines of the file “bar”
![Page 24: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/24.jpg)
sort
Sort line of text filecat numbers
2314 5678
345 2231
101 984
4842 6543
98 11001
[root] sort numbers101 9842314 5678345 22314842 654398 11001
[root] sort -n numbers98 11001101 984345 22312314 56784842 6543Numeric order
Stringorder
![Page 25: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/25.jpg)
sort examples
cat numbers2314 5678
345 2231
101 984
4842 6543
98 11001
sort numbers101 9842314 5678345 22314842 654398 11001
sort -k2 numbers98 11001345 22312314 56784842 6543101 984
Sort by the 1st field
Sort bythe 2nd field
![Page 26: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/26.jpg)
sort examples
cat numbers
2314 5678
345 2231
101 984
4842 6543
98 11001
sort numbers101 9842314 5678345 22314842 654398 11001
sort -r numbers98 110014842 6543345 22312314 5678101 984
Sort inreverseorder
![Page 27: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/27.jpg)
diff
Display the differences between two files
Syntax: diff [options] file1 file2
When using “diff” without any options, it produces a series of lines containing:
Add (a) Delete (d), and Change (c) instructions
Each of these lines is followed by the lines from the file that you need to add, delete, or change to make the files the same.
![Page 28: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/28.jpg)
diff examples
cat file1
blue
red
white
yellow
orange
cat file2blueyellowblackredorange
$diff file1 file22,3d1< red< white4a3,4> black> red
1. Delete line 2 through 3 from file1
2. Append lines 3 through 4 from file2 after line 4 in file1
Steps to convert file1 to file2:
![Page 29: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/29.jpg)
file
Displays the classification (type) of a file. Considered useful if a file extension is missing, or if user is unsure of file extension.
Syntax: file [option] file-list
Examples:file assign01.html
assign01.html: HTML document text
file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
file mydoc.doc
mydoc.doc: Microsoft Office Document
file 1
1: empty
![Page 30: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/30.jpg)
who
Shows which users are logged on to serverPhobos: /home/rchan>$ who
rchan pts/0 Oct 30 02:08 (toronto-hse-ppp3)
sslui pts/1 Oct 30 01:11 (CPE00112f0fe590-)
Phobos: /home/rchan>$ who -H
Name Line Time Hostname
rchan pts/0 Oct 30 02:08 (toronto-hse-ppp3)
sslui pts/1 Oct 30 01:11 (CPE00112f0fe590-)
Phobos: /home/rchan>$ who -qH
Name Hostname
rchan (toronto-hse-ppp3)
sslui (CPE00112f0fe590-)
Total users: 2
![Page 31: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/31.jpg)
who options
Major options for grep:
-H displays column headings
-T includes message reception status: + message reception on - message reception off
-i includes column indicating number of minutes
of user inactivity.
![Page 32: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/32.jpg)
who examples
Show who is logged on who -H
Name Line Time Hostname
root pts/0 Mar 02 09:11 (142.204.20.17)
msaul pts/1 Mar 02 09:21 (CPE0040f4df2fef-)
who -i
root pts/0 Mar 02 09:11 0:03 42368 (142.204.20.17)
msaul pts/1 Mar 02 09:21 . 37790
who -T
root - pts/0 Mar 02 09:11
msaul + pts/1 Mar 02 09:21
Less than 1 minute inactivity
Allows message reception
Column headings
![Page 33: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/33.jpg)
date
Displays the system time and date[ray@localhost week8]$ date
Sun Oct 30 01:48:10 EST 2005
[ray@localhost week8]$ date +"%D"
10/30/05
[ray@localhost week8]$ date +"%T"
01:54:05
[ray@localhost week8]$ date +"%D %T"
10/30/05 01:54:13
Refer to the man page for more formatting codes
![Page 34: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/34.jpg)
which
Shows the full path of (shell) commands
[ray@localhost week8]$ which mkdir
/bin/mkdir
[ray@localhost week8]$ which type
/usr/bin/which: no type in (/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/ray/bin)
![Page 35: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/35.jpg)
finger
User information lookup program
Phobos: /home/rchan>$ finger rchan
Login name: rchan In real life: Raymond Chan
Directory: /home/rchan Shell: /usr/bin/ksh
On since Oct 30 02:08:55 on pts/0
from toronto-hse-ppp3
(messages off)
No Plan.
![Page 36: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/36.jpg)
Send and receive mail To read your mail on phobos, type the “mail”
command by itself:
Phobos: /home/rchan>$ mail
Mail [5.2 UCB] [AIX 4.1] Type ? for help.
"/var/spool/mail/rchan": 1 message 1 new
>N 1 rchan Wed Oct 26 00:24 10/340 "Mail testing"
![Page 37: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/37.jpg)
Sending mail
To send a file called “letter” through email on
phobos to the user “rchan”:
Phobos: /home/rchan>$ mail -s “subject” rchan < letter
![Page 38: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/38.jpg)
lpr, lpq, lprm
lpr – submit file for printing
lpr [ -P printer-name ] [ -# copies ] file-name
[ -P printer-name] : send files to the named printer
[ -# copies ] :sets the number of copies to print between 1 and 100file-name : name of file to be printed
![Page 39: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/39.jpg)
printer queue status
lpq - show printer queue status
lpq [ -P printer-name] [ -a ] [ -l]
[ -P printer-name] : show status on the named printer
[ -a ] : reports jobs on all printers[ -l ] : display more verbose (long) format
![Page 40: Agenda - Seneca Collegemurray.saul/BIF713/... · Use egrep or grep –E when using grep with extended regular expressions. ... NOTE: There are some short-cut symbols: ... Example:](https://reader034.vdocuments.net/reader034/viewer/2022042211/5eb43ec65f2ff147843b266f/html5/thumbnails/40.jpg)
Cancel Print Jobs
lprm – cancel print jobs
lprm [ - ] [ -P printer-name] [ job ID(s)]
[ - ] : all print jobs[ -P printer-name] : print jobs on the named
printer[ job ID(s) ]: jobs to be cancel