Command |
Description |
sh-2.02$ cat cars
plym fury 77 73 2500
chevy nova 79 60 3000
ford mustang 65 45 10000 ford
volvo gl 78 102 9850
ford ltd 83 15 10500
chevy nova 80 50 3500
fiat 600 65 115 450
honda accord 81 30 6000
ford thundbd 84 10 17000
toyota tercel 82 180 750
chevy impala 65 85 1550
ford bronco 83 25 9500
|
The cars file
|
sh-2.02$ awk '/chevy/' cars
chevy nova 79 60 3000
chevy nova 80 50 3500
chevy impala 65 85 1550
|
Look for pattern chevy.
|
sh-2.02$ awk '{print $3, $1} ' cars
77 plym
79 chevy
65 ford
78 volvo
83 ford
80 chevy
65 fiat
81 honda
84 ford
82 toyota
65 chevy
83 ford
|
Print column 3 followed by a space, followed by column 1
|
sh-2.02$ awk '/chevy/ {print $3 $1}' cars
79chevy
80chevy
65chevy
|
Print column 3 followed by column 1
|
sh-2.02$ awk '/chevy/ {print $3, $1}' cars
79 chevy
80 chevy
65 chevy
|
For those lines with chevy in them,
print column 3 space by column 1
|
sh-2.02$ awk '/h/' cars
chevy nova 79 60 3000
chevy nova 80 50 3500
honda accord 81 30 6000
ford thundbd 84 10 17000
chevy impala 65 85 1550
|
Print records with an "h" in them
|
sh-2.02$ awk '$1 ~ /h/' cars
chevy nova 79 60 3000
chevy nova 80 50 3500
honda accord 81 30 6000
chevy impala 65 85 1550
|
Print records where the first column contains an "h"
|
sh-2.02$ awk '$1 ~ /^h/' cars
honda accord 81 30 6000
|
Print records where the first column begins with an "h"
|
sh-2.02$ awk '$2 ~ /^[tm]/ {print $3, $2, "$" $5}' cars
65 mustang $10000
84 thundbd $17000
82 tercel $750
|
Print column3 column2 $column5 for those records that start with a t or m.
|
sh-2.02$ awk '$3 ~ /5$/ {print $3, $1, "$" $5}' cars
65 ford $10000
65 fiat $450
65 chevy $1550
|
Print column3 column2 $column5 for those records where clumn 3 ends with a 5.
|
sh-2.02$ awk '$3 == 65' cars
ford mustang 65 45 10000 ford
fiat 600 65 115 450
chevy impala 65 85 1550
|
Print those records where column three is the number 65.
|
sh-2.02$ awk '$5 <= 3000' cars
plym fury 77 73 2500
chevy nova 79 60 3000
fiat 600 65 115 450
toyota tercel 82 180 750
chevy impala 65 85 1550
|
Print those records where column five is numerically less than or equal to 3000.
|
sh-2.02$ awk '$5 >= "2000" && $5 < "9000"' cars
plym fury 77 73 2500
chevy nova 79 60 3000
chevy nova 80 50 3500
fiat 600 65 115 450
honda accord 81 30 6000
toyota tercel 82 180 750
|
Print those records where column five is textually between 2000 and 9000.
|
sh-2.02$ awk '$5 >= 2000 && $5 < 9000' cars
plym fury 77 73 2500
chevy nova 79 60 3000
chevy nova 80 50 3500
honda accord 81 30 6000
|
Print those records where column five is numerically between 2000 and 9000.
|
sh-2.02$ awk '/volvo/ , /fiat/' cars
volvo gl 78 102 9850
ford ltd 83 15 10500
chevy nova 80 50 3500
fiat 600 65 115 450
|
Print those records from the line that has volvo on it until the line with
fiat on it.
|
sh-2.02$ awk '/chevy/ , /ford/' cars
chevy nova 79 60 3000
ford mustang 65 45 10000 ford
chevy nova 80 50 3500
fiat 600 65 115 450
honda accord 81 30 6000
ford thundbd 84 10 17000
chevy impala 65 85 1550
ford bronco 83 25 9500
|
Print those records from the line that has chevy on it until the line with
ford on it. Notice that multiple such occurences exist.
|