120 likes | 229 Views
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]<pattern><files> Selects and outputs lines from file(s) that match a specific pattern
E N D
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]<pattern><files> • 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/<oldstring>/<newstring>/g’<file> Deletion = sed ‘<start>,<end>d’ <file> • 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 <pattern>”{print something}’<file> • 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 <flags><sort fields><filename> • 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 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 2nd field. Standard out: Jack Arta 800000 Indonesia Bay Ching 5000000 China Cruella Lumper 725000 Malaysia
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
CIS240 – UNIX File Systems cut <pattern><file> • 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]<filelist> • Concatenates horizontally