Awk class

advertisement
15/2/16
106735846
awk ‫דף עבודה בנושא‬
> awk 'program' filename
:'‫תחביר א‬
The following examples work with the cars data file. From left to right, the columns in the file
contain each car's producer, model, year of manufacture, mileage in 1000's, and price. All white
space in this file is composed of single TABs (there are no SPACEs in the file).
plym
chevy
ford
volvo
ford
chevy
fiat
honda
ford
toyota
chevy
ford
fury
nova
mustang
gl
ltd
nova
600
accord
thunbd
tercel
impala
bronco
77
79
65
78
83
80
65
81
84
82
65
83
73
60
45
102
15
50
115
30
10
180
85
25
2500
3000
10000
9850
10500
3500
450
6000
17000
750
1550
9500
1. This example selects all lines that contain the string chevy. The slashes indicate that chevy
is a string. This example has no action part.
> awk '/chevy/' cars
chevy
chevy
chevy
nova
nova
impala
79
80
65
60
50
85
3000
3500
1550
2. 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 part so
that awk can distinguish the pattern part from the action part. This example prints the third
field ($3) and the first field ($1) of each selected line.
> awk '{print $3, $1}' cars
77
79
65
78
83
80
65
77
81
84
82
65
83
plym
chevy
ford
volvo
ford
chevy
fiat
plym
honda
ford
toyota
chevy
ford
3. 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.
Nathan Garber,1998
1/4
Joblist #2 for AWK
15/2/16
106735846
> awk '/chevy/ {print $3, $1}' cars
79
80
chevy
chevy
chevy
65
4. The next example selects lines that contain a match for the string h. Because there is no
explicit action, it prints all the lines it selects.
> awk '/h/' cars
5. The next pattern uses the matches operator (~) to select all lines that contain the letter h in
the first field:
> awk '$1 ~ /h/' cars
chevy
chevy
honda
chevy
nova
nova
accord
impala
79
80
81
65
60
50
30
85
3000
3500
6000
1550
6. The caret ( ^ ) before a string forces a match at the beginning of the line or, in this case, the
beginning of the first field:
> awk '$1 ~ /^h/' cars
honda
accord
81
30
6000
7. Next awk selects all lines that have a second field that begins with t or m. Then it prints the
third and second fields, a dollar sign, and the fifth field.
> awk '$2 ~ /^[tm]/ {print $3, $2, "$" $5}' cars
65
84
82
mustang
thunbd
tercel
$10000
$17000
$750
8. The next example shows three roles that a dollar sign can play in an awk program. A dollar sign
followed by a number forms the name of the field. After a string, a dollar sign forces a match at the
end of a line or a field (5$). Within a string , you can use a dollar sign as itself.
> awk '$3 ~ /5$/ {print $3, $1, "$" $5}' cars
65
65
65
ford
fiat
chevy
$10000
$450
$1550
9. Equational operator "is equal" (= =) causes awk to perform a numeric comparison between
the third field in each line and a number 65. The awk command takes the default action, Print,
on each line that matches.
> awk '$3 = = 65' cars
Nathan Garber,1998
2/4
Joblist #2 for AWK
15/2/16
ford
fiat
chevy
106735846
mustang 65
600
65
impala
65
45
115
85
10000
450
1550
10. The next example finds all cars priced at or under $3000:
> awk '$5 <= 3000' cars
plym
chevy
fiat
toyota
chevy
fury
nova
600
tercel
impala
> awk -f
77
79
65
82
65
73
60
115
180
85
progfile
2500
3000
450
750
1550
filename
:'‫תחביר ב‬
When you are writing a longer awk program, it is convenient to put the program in a file and
reference the file on the command line. Use the -f option, followed by the name of the file
containing the awk program.
11. The following awk program named pr_header has two actions and uses the BEGIN
pattern. The awk utility performs the action associated with BEGIN before it processes any of
the lines of the data file: it prints a header. The second action {print}, has no pattern part and
prints all the lines in the file.
> more pr_header
BEGIN
{print "Make
Model Year Miles Price"
print "-----------------------------------------------" }
{print}
> awk -f pr_header cars
Make
Model
Year Miles Price
-------------------------------------------------plym
chevy
ford
volvo
ford
chevy
fiat
honda
ford
toyota
chevy
ford
fury
nova
mustang
gl
ltd
nova
600
accord
thunbd
tercel
impala
bronco
77
79
65
78
83
80
65
81
84
82
65
83
73
60
45
102
15
50
115
30
10
180
85
25
2500
3000
10000
9850
10500
3500
450
6000
17000
750
1550
9500
12. The NR variable contains the number )‫ (מספר סידורי‬of the current line. The following
pattern selects all lines that contain more than 23 characters. The action prints the line number
of all the selected lines.
> awk 'length > 23 {print NR}' cars
3
10
Nathan Garber,1998
3/4
Joblist #2 for AWK
15/2/16
106735846
13. You can combine the range operator (,) and the NR variable to display a group of lines of a
file based on their line numbers. The next examples displays lines 2 through 4.
> awk 'NR == 2, NR == 4' cars
chevy
ford
volvo
nova
mustang
gl
79
65
78
60
45
102
3000
10000
9850
14. The next example uses if commands to change the values of some of the first fields. As
long as awk does not any changes to a record, it leaves the entire record, including separators,
intact. Once it make a change to a record.
> more separ_demo
{ if ($1 ~ /ply/) $1 = "plymouth"
if ($1 ~ /chev/) $1 = "chevrolet"
print }
> awk -f separ_demo cars
plymouth
fury
77
chevrolet
nova 79
ford
mustang 65
volvo
gl
78
ford
ltd
83
chevrolet
nova 80
fiat
600
65
honda
accord
81
ford
thunbd
84
toyota
tercel
82
chevrolet
impala
ford
bronco
83
Nathan Garber,1998
73
60
45
102
15
50
115
30
10
180
65
25
2500
3000
10000
9850
10500
3500
450
6000
17000
750
85
1550
9500
4/4
Joblist #2 for AWK
Download