CS435-eg-d - 540 Part ll The U NlX Utility Programs cfw_...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
540 Part ll The UNlX Utility Programs { print 3 This program consists of one program line that is an action. It uses no pattern. Because the pattern is missing, awk selects all lines in the input file. Without any arguments, the Print command prints each selected line in its entirety. This program copies the input file to its standard output. he foliowing program has a pattern part without an explicit action. In this case, awk selects all lines from the input file that contain the string jenny. When you do not specify an action, awk assumes the action to be Print. This program copies all the lines in the input file that contain jenny to its standard output. The following examples work with the cars data file. From left to right, the columns In the file contain each car's make, model, year of manufacture, mileage, and price. All white space in this file is composed of single ms (there are no 0s in the file). $ cat cars plym fury 77 chevy nova 79 ford mustang 65 volvo gl 78 Itd 8 3 80 fi at 600 65 honda accord 81 thundbd 84 toyota tercel 82 chevy impala bronco 83 The first example below selects all lines that contain the string chevy. The slashes indicate that chevy is a regular expression. This example has no action part. Although neither awk nor shell syntax requires single quotation marks on the command line, it is a good idea to use them, because they prevent many problems. If the awk program you create on the command line in- cludes (spncEls or any special characters that the shell will interpret, you must quote them. Always enclosing the program in single quotation marks is the easiest way of making sure you have quoted any characters that need to be quoted. $ awk '/chevy/' cars 79 60 3000 80 50 3500 85 1550 The next example selects all lines from the file (it has no pattern part). The braces enclose the action part-you must always use braces to delimit the action Dart. so that awk can distinrmish the Dattem Dart from the action
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
part. This example prints the third field ($3), a (SPACO (indicated by the com- ma), and the first field ($1) of each selected line. $ awk '{print $3, $1)' cars 77 plym 79 chevy 65 ford 78 volvo 83 80 fiat 81 honda 84 82 toyota chevy The next example includes both a pattern and an action part. It selects all lines that contain the string chevy and prints the third and first fields from the lines it selects. $ awk '/chevy/ {print $3, $1)' cars 79 chevy chevy chevy The next example selects lines that contain a match for the regular ex- pression h. Because there is no explicit action, it prints all the lines it selects. $ awk '/h/' nova . 79 60 nova 5 0 honda accord 30 thundbd 10 17000 impala 85 1550 The next pattern uses the matches operator (-) to select all lines that contain the letter h in the first field. $ awk '$1 - /h/' chevy nova chevy nova 50 honda chevy impal a The caret (") in a regular expression forces a match at the beginning of the line or, in this case, the beginning of the first field.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

CS435-eg-d - 540 Part ll The U NlX Utility Programs cfw_...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online