Class Notes (839,075)
Canada (511,182)
CSC209H1 (41)
Lecture

L2 - Commands.pdf

12 Pages
99 Views

Department
Computer Science
Course Code
CSC209H1
Professor
Dan Dolderman

This preview shows pages 1,2 and half of page 3. Sign up to view the full 12 pages of the document.
Description
SOFTWARE TOOLS In addition to the basic UNIX commands, we discuss some software tools that you will find useful. Absolute Basics You must know these commands: Command Description cd change current working directory pwd print current working directory mkdir creates a directory rmdir removes directory, must be empty rm removes a file. cp copies files/dirs mv file/divses chmod change file permissions – discussed in class Basics These were discussed in the assigned exercises last week. Command Description ls lists files – more discussion to come in class cat displays a file more like cat, but displays a file 1 screen at a time ps lists processes kill kills an active process who displays currently logged on users finger displays more details about a user su switch accounts du disk usage head, tail display 1 /last couple of lines wc counts the number of lines,words, characters in a file lpr, lpq, lprm printing related commands clear clears the screen Advanced Tools These tools are very useful and will be discussed in some detail below: Command Description grep,egrep search file(s) for strings sort sorts the contents of files uniq eliminates duplicate lines find search for files and much more tar archive files/dirs diff show change(s) made to a file by comparing to new version compress, compress/decompress files uncompress, gzip, gunzip sed stream editor – transforms text as it streams through grep & regular expressions – search for strings grep stands for “global regular expression print”. It’s a very useful tool you can use to search for occurrences of strings in files. Before you can use grep, you need to understand regular expressions. Regular Expressions A regular expression is a sequence of ordinary characters and special characters. Special characters include the following: back slash (\), caret (^), asterisk (*), dollar ($), square brackets ([]), and dot (.). A simple string The simplest of regular expressions is just a string. If you give grep a string with no special characters, it’ll match whenever it finds the string, even if other characters surround it. So, grep con socket*.cpp will search all files beginning with socket and ending with .cpp for the string con. con would match connect, continue, connection, lexicon ...etc. werewolf:~% grep 'con' socket*.cpp socketclient.cpp: if( connect(sockfd,(struct sockaddr *)&serve r,SIZE)==-1) socketclient.cpp: printf("connect call fails\n"); socketserv.cpp: printf("waiting for connection...\n"); socketserv.cpp: continue; socketserv.cpp: //parent should close the new socket connection. werewolf:~% Special characters dot (.) matches any single character. use dot (.) to match any single character. For example, w.r would match the strings lowercase, worship or warning. restrict ranges with [] If you want to restrict the range of matches, use the square brackets: w[ea]r matches only lowercase and warning, but not worship. It’s a good idea to pass the regular expression to grep surrounded by a pair of single quotes, so the shell doesn’t do any substitutions on your regular expression. So use grep 'w[ea]r' myfile st If the 1 character in the square bracket is the caret (^), then the match occurs on characters NOT in the list. You can specify a range of characters with -. grep 'w[a-z]r' mmatches any lower case character between w and r. match a specific number of a single character use a pair of escaped braces to specify the minimum and maximum number of a single character: n\{2,4\matches nn, nnn, or nnnn. If you specify only 1 number, then the exact number of occurrences will be made: werewolf:~% grep 'n\{2\}' socket*.cpp socketclient.cpp: if( connect(sockfd,(struct sockaddr *)&serve r,SIZE)==-1) socketclient.cpp: printf("connect call fails\n"); socketserv.cpp: printf("waiting for connection...\n"); socketserv.cpp: //parent should close the new socket connection. werewolf:~% * matches zero or more of the preceding expression grep 'p*' myfdisplays the entire file, since every line has 0 or more p. grep 'ppp*' myfdisplays all lines that have at least 2 ‘p’. ^ anchors in front, $ anchors at the end grep '^[Tt]he'displays all the lines beginning with ‘the’ or ‘The’ grep '\.$'displays all lines that end with a period. st Actually, its probably more accurate to say that ^ matches the imaginary character before the 1 in the line, and $ matches the imaginary character at the end of the line. egrep egrep provides a few more ways you can match stuff. Most notably, + is like *, but matches 1 or more occurrences, and ? matches 0 or 1 occurrences of anything. egrep also allow you to search for either of two regular expressions using the | operator. egrep 'dd+|socket' sdisplays all lines that have at least 2 ds or the word socket in it. Sort Consider the following raw results from a swim meet: Name Age Team Prelim Final Stanford, Jeffrey 25 HIMA 47.07 46.32 Liggett, Michael 27 DYNA 47.25 48.12 Baker, Chase 29 GMUP 56.28 57.79 Kittredge, Brad 25 TOC 45.05 46.22 Richner, Thomas 27 UCLA 50.00 48.79 McCormick, Aaron 27 RMM 49.00 49.30 Thorum, Thomas 29 UTAH 50.00 49.45 Paul, Darcy 26 NEM 52.00 50.17 Welting, Evan 27 DYNA 50.50 51.04 Wanie, Lee 28 TOC 46.00 46.39 Linderman, Ross 25 PNA 52.55 51.17 Wen, Patrick 29 UCLA 57.50 55.93 Frohlich, Jon 29 UTAH 49.10 49.20 Parnes, Jason 29 SDSM 63.00 59.11 Perunovich, Steven 27 HIMA 59.50 52.93 You might want to sort the file according to certain criteria. Suppose the results are stored in a file called swimresults. sort lines Invoke sort with no arguments just sorts the lines: $ sort swimresults Baker, Chase 29 GMUP 56.28 57.79 Frohlich, Jon 29 UTAH 49.10 49.20 Kittredge, Brad 25 TOC 45.05 46.22 Liggett, Michael 27 DYNA 47.25 48.12 Linderman, Ross 25 PNA 52.55 51.17 McCormick, Aaron 27 RMM 49.00 49.30 Parnes, Jason 29 SDSM 63.00 59.11 Paul, Darcy 26 NEM 52.00 50.17 Perunovich, Steven 27 HIMA 59.50 52.93 Richner, Thomas 27 UCLA 50.00 48.79 Stanford, Jeffrey 25 HIMA 47.07 46.32 Thorum, Thomas 29 UTAH 50.00 49.45 Wanie, Lee 28 TOC 46.00 46.39 Welting, Evan 27 DYNA 50.50 51.04 Wen, Patrick 29 UCLA 57.50 55.93 $ Since the last names were listed first, sorting the lines is equivalent to sorting by last names. Note the sort is performed by comparing ASCII values. As a result, upper case letters are “less” than lower case letters and space is “less” than a character. Skip fields with + What would I do if I wanted to sort on the age of the participants instead of the last name? This can be accomplished by telling sort to skip the first 2 fields (last name, first name): $sort +2 swimresults Wanie, Lee 28 TOC 46.00 46.39 Paul, Darcy 26 NEM 52.00 50.17 Baker, Chase 29 GMUP 56.28 57.79 Wen, Patrick 29 UCLA 57.50 55.93 Welting, Evan 27 DYNA 50.50 51.04 Parnes, Jason 29 SDSM 63.00 59.11 Frohlich, Jon 29 UTAH 49.10 49.20 Thorum, Thomas 29 UTAH 50.00 49.45 Linderman, Ross 25 PNA 52.55 51.17 Kittredge, Brad 25 TOC 45.05 46.22 Richner, Thomas 27 UCLA 50.00 48.79 Liggett, Michael 27 DYNA 47.25 48.12 McCormick, Aaron 27 RMM 49.00 49.30 Stanford, Jeffrey 25 HIMA 47.07 46.32 Perunovich, Steven 27 HIMA 59.50 52.93 ...hmm, that didn’t seem to work too well. The problem is that sort treats the 1 white space as the field separator. The rest of the field, including all the rest of the white spaces are factored into the sort. Because the age field included an uneven number of leading spaces, those spaces where sorted too, leading to the problem we see. Skip leading spaces with –b Try this again with the –b option. This tells sort to ignore leading spaces. % sort -b +2 swimresults Stanford, Jeffrey 25 HIMA 47.07 46.32 Linderman, Ross 25 PNA 52.55 51.17 Kittredge, Brad 25 TOC 45.05 46.22 Paul, Darcy 26 NEM 52.00 50.17 Liggett, Michael 27 DYNA 47.25 48.12 Welting, Evan 27 DYNA 50.50 51.04 Perunovich, Steven 27 HIMA 59.50 52.93 McCormick, Aaron 27 RMM 49.00 49.30 Richner, Thomas 27 UCLA 50.00 48.79 Wanie, Lee 28 TOC 46.00 46.39 Baker, Chase 29 GMUP 56.28 57.79 Parnes, Jason 29 SDSM 63.00 59.11 Wen, Patrick 29 UCLA 57.50 55.93 Frohlich, Jon 29 UTAH 49.10 49.20 Thorum, Thomas 29 UTAH 50.00 49.45 That’s more like it. Use stop field number with – By default, sort includes the rest of the fields in the sort. So ifsort -b +2 swimr, the rest of the line, such as 25 HIMA 47.07 46.32 rd is used for the sort. You can tell sort not to include rest of the fields, for example, to sort on the 3 field only. This can be done with the stop field operator –. The – operator tells sort to ignore everything after the field indicated. Sort -b +2 -3 swimresults says: skip the first 2 fields, ignore all leading blanks, rd and ignore everything after the 3eld. Contrast the difference betwsort -b +2 -3 swimresults and sort -b +2 swimresults : werewolf:~% sort -b +2 -3 swimresults Kittredge, Brad 25 TOC 45.05 46.22 Linderman, Ross 25 PNA 52.55 51.17 Stanford, Jeffrey 25 HIMA 47.07 46.32 Paul, Darcy 26 NEM 52.00 50.17 Liggett, Michael 27 DYNA 47.25 48.12 McCormick, Aaron 27 RMM 49.00 49.30 Perunovich, Steven 27 HIMA 59.50 52.93 Richner, Thomas 27 UCLA 50.00 48.79 Welting, Evan 27 DYNA 50.50 51.04 Wanie, Lee 28 TOC 46.00 46.39 Baker, Chase 29 GMUP 56.28 57.79 Frohlich, Jon 29 UTAH 49.10 49.20 Parnes, Jason 29 SDSM 63.00 59.11 Thorum, Thomas 29 UTAH 50.00 49.45 Wen, Patrick 29 UCLA 57.50 55.93 werewolf:~% sort -b +2 swimresults Stanford, Jeffrey 25 HIMA 47.07 46.32 Linderman, Ross 25 PNA 52.55 51.17 Kittredge, Brad 25 TOC 45.05 46.22 Paul, Darcy 26 NEM 52.00 50.17 Liggett, Michael 27 DYNA 47.25 48.12 Welting, Evan 27 DYNA 50.50 51.04 Perunovich, Steven 27 HIMA 59.50 52.93 McCormick, Aaron 27 RMM 49.00 49.30 Richner, Thomas 27 UCLA 50.00 48.79 Wanie, Lee 28 TOC 46.00 46.39 Baker, Chase 29 GMUP 56.28 57.79 Parnes, Jason 29 SDSM 63.00 59.11 Wen, Patrick 29 UCLA 57.50 55.93 Frohlich, Jon 29 UTAH 49.10 49.20 Thorum, Thomas 29 UTAH 50.00 49.45 Secondary sorts The stop field operator is useful for performing secondary sorts. Suppose I wanted to list the fastest people according to age groups: werewolf:~% sort -b +2 -3 +5 swimresults Kittredge, Brad 25 TOC 45.05 46.22 Stanford, Jeffrey 25 HIMA 47.07 46.32 Linderman, Ross 25 PNA 52.55 51.17 Paul, Darcy 26 NEM 52.00 50.17 Liggett, Michael 27 DYNA 47.25 48.12 Richner, Thomas 27 UCLA 50.00 48.79 McCormick, Aaron 27 RMM 49.00 49.30 Welting, Ev
More Less
Unlock Document

Only pages 1,2 and half of page 3 are available for preview. Some parts have been intentionally blurred.

Unlock Document
You're Reading a Preview

Unlock to view full version

Unlock Document

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit