CIS 240 Introduction to UNIX Instructor: Sue Sampson
CIS240 – UNIX File Systems Regular Expressions are used in: grep sed awk sort cut paste
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…
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
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
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
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
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
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)
CIS240 – UNIX File Systems File donations.txt Bay Ching China Jack Arta Indonesia Cruella Lumper Malaysia Results of an sort: sort 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 Indonesia Bay Ching China Cruella Lumper Malaysia
CIS240 – UNIX File Systems File donations.txt Bay Ching China Jack Arta Indonesia Cruella Lumper Malaysia Results of an sort: sort -r 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 Indonesia Cruella Lumper Malaysia Bay Ching China
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