1 / 12

CIS 240 Introduction to UNIX Instructor: Sue Sampson

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

alder
Download Presentation

CIS 240 Introduction to UNIX Instructor: Sue Sampson

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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]<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…

  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/<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

  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 <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

  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 <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)

  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 2nd 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 <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

More Related