Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 403: Programming Languages

Similar presentations


Presentation on theme: "CS 403: Programming Languages"— Presentation transcript:

1 CS 403: Programming Languages
Lecture 22 Fall 2003 Department of Computer Science University of Alabama Joel Jones

2 Overview Announcements Puzzle from last time sed awk
Story Hour, Houser 108, 3PM Friday Puzzle from last time sed awk Lecture 22

3 Printing 10 most common words
cat $* | # tr doesn’t take filename arguments tr -sc A-Za-z ‘\012’ | # all non alpha become newline sort | uniq -c | # get the count sort -n | # sort by count tail # prints 10 by default Use the man command to look for help man sort Lecture 22

4 sed sed [options]‘list of commands’ filenames … Commands s/re1/re2/ substitute regular expression r1 with r2, first instance on every line s/re1/re2/g substitute regular expression r1 with r2, every instance on every line #command, does command for # times E.g. sed 3q prints first 3 lines /re1/q prints lines up to first one matching re1, then quits Lecture 22

5 sed (cont.) sed ‘s/^/ /’ file More commands sed ‘/./s/^/ /’ file # or
Pair Up: Write a sed command line that indents a line by adding four spaces to the beginning of the line sed ‘s/^/ /’ file More commands /re1/s/re2/re3/ substitute regular expression re2 with re3, first instance on every line matching re1 Pair Up: What does the above sed command do with empty lines? Write a sed command line that fixes this problem. sed ‘/./s/^/ /’ file # or sed ‘/^$/!s/ /’ file Lecture 22

6 sed (cont.) Commands (cont.) Options -n turns off automatic printing
/re/d deletes lines matching re /re/p prints lines matching re Pair Up: What does the sed command sed ‘/the/p’ < file do? Options -n turns off automatic printing -f filename takes sed commands from filename Pair Up: Write a sed command line that does the same thing as: grep re file sed -n ‘/re/p’ file Lecture 22

7 awk awk [options]‘program’ filenames … Like sed, but program is different: pattern { action } pattern { action } … awk reads input in filenames one line at a time & when pattern matches, executes corresponding action Patterns Regular expressions C-like expressions Lecture 22

8 awk (cont.) awk ‘/re/’ file awk ‘{ print }’ filenames …
Pattern or action is optional Pattern missing—perform action on every line Action missing—print every line matching pattern Simple action print—without argument prints current line Pair Up: Write an awk command line that does the same thing as: grep re file awk ‘/re/’ file Pair Up: Write an awk command line that does the same thing as: cat filenames … awk ‘{ print }’ filenames … Lecture 22

9 awk (cont.) Variables Options $0—entire line, $1-$NF—fields of line
E.g. awk ‘{ print $2 }’ textFile prints 2nd field of every line of textFile E.g. who | awk ‘{ print $5, $1 }’ | sort prints name and login sorted by time NF is number of fields on current line NR is number of records (lines) read so far Options -Fchar—sets char as the field separator Pair Up: Write an awk command line that prints user names out of /etc/passwd, where the user name is the first field and fields are colon separated. Lecture 22

10 awk (cont.) awk -F: ‘{ print $1 }’ /etc/passwd Field breaking
N.B. most unix systems don’t store users in /etc/passwd anymore Field breaking Default is on space and tab and multiple contiguous white space counts as a single white space and leading separators are discarded Setting separator causes leading separators to be counted Lecture 22

11 awk (cont.) More on patterns awk ‘NF % 2 != 0’ files
Print user name of people who have no password $2 == “” nd field is empty $2 ~ /^$/ 2nd field matches empty string $2 !~ /./ 2nd field doesn’t match any character length($2) == Length of 2nd field is zero Pair Up: Write an awk command line that prints lines of input that have an odd number of fields. awk ‘NF % 2 != 0’ files Pair Up: Write an awk command line that is the shortest equivalent of cat. Lecture 22


Download ppt "CS 403: Programming Languages"

Similar presentations


Ads by Google