Download presentation
Presentation is loading. Please wait.
1
CIS 240 Introduction to UNIX Instructor: Sue Sampson
2
CIS240 – UNIX File Systems Regular Expressions are used in: grep sed awk sort cut paste
3
CIS240 – UNIX File Systems grep [flags] Selects and outputs lines from file(s) that match a specific pattern Requires a regular expression for the pattern Pattern examples: abc = match lines containing “abc” ^abc = match lines starting with “abc” abc$ = match lines ending with “abc” Flag examples: -i = ignore case -v = output lines that do not match…
4
CIS240 – UNIX File Systems File poem.txt Mary had a little lamb Mary fried a lot of spam Jack ate a Spam sandwich Jill had a lamb spamwich Results of a grep: grep spam poem.txt ----only goes to standard out… Mary fried a lot of spam Jill had a lamb spamwich
5
CIS240 – UNIX File Systems Substitution = sed ‘s/ / /g’ Deletion = sed ‘, d’ Changes all occurrences of one string within a file that matches a specific pattern Requires a regular expression for the pattern Does not change original file s = subsitute g = globally To save changes, provide name of new file
6
CIS240 – UNIX File Systems File poem.txt Mary had a little lamb Mary fried a lot of spam Jack ate a Spam sandwich Jill had a lamb spamwich Results of a sed: sed 2,3d poem.txt ---only goes to standard out… Mary had a little lamb Jill had a lamb spamwich Results of a sed: sed 2,3d poem.txt>poemmod.txt ---saved to poemmod.txt Mary had a little lamb Jill had a lamb spamwich
7
CIS240 – UNIX File Systems awk ”{print something}’ Merge of data and a template Transforms each line of an input file As awk processes each line, it assigns variable names; $1, $2, etc… Supports regular expressions Does not change original file
8
CIS240 – UNIX File Systems File words.txt nail hammer wood pedal foot car clown pie circus Results of an awk: awk ‘{print “Hit the”,$1,”with your”,$2}’ words.txt File words.txt Hit the nail with your hammer Hit the pedal with your foot Hit the clown with your pie
9
CIS240 – UNIX File Systems sort Sorts according to placement of field Can specify delimiter Does not change original file Redirect if you want changes saved Flag examples: -f = all lines become uppercase -r = sort in reverse order -tx = used x as the delimiter (can be, ; etc)
10
CIS240 – UNIX File Systems File donations.txt Bay Ching 5000000 China Jack Arta 800000 Indonesia Cruella Lumper 725000 Malaysia Results of an sort: sort +1 -2 donations.txt --start at the first character of the 1+1st field, end at the last character of the 2 nd field. Standard out: Jack Arta 800000 Indonesia Bay Ching 5000000 China Cruella Lumper 725000 Malaysia
11
CIS240 – UNIX File Systems File donations.txt Bay Ching 5000000 China Jack Arta 800000 Indonesia Cruella Lumper 725000 Malaysia Results of an sort: sort -r +2 -3 donations.txt>donationsort.txt --start at the first character of the 1+2nd field, end at the last character of the 3rd field, sort in reverse, save to donationsort.txt File donationsort.txt Jack Arta 800000 Indonesia Cruella Lumper 725000 Malaysia Bay Ching 5000000 China
12
CIS240 – UNIX File Systems cut Selects and outputs lines from file(s) that match a specific pattern, specified by column or field Requires a regular expression for the pattern Pattern examples: -c1-6 = print all lines, columns 1-6 d: -f3 = print all lines, field 3, delimiter is : paste [options] Concatenates horizontally
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.