Skip to content

Instantly share code, notes, and snippets.

@bensmithett
Created July 9, 2018 01:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bensmithett/8a2df73b47a5577ebe886a3eb8aeeb58 to your computer and use it in GitHub Desktop.
Save bensmithett/8a2df73b47a5577ebe886a3eb8aeeb58 to your computer and use it in GitHub Desktop.
Learning awk
$ awk '/Jade/ && !/English/' ./students.csv
5553,Jade,Mathematics,100,,,
$ awk '/Jade/' ./students.csv
5553,Jade,English,73,,,
5553,Jade,Mathematics,100,,,
$ awk '/Jade/ {print "****"}' ./students.csv
****
****
$ awk '/Jade/ {print "****"; print $1}' ./students.csv
****
5553,Jade,English,73,,,
****
5553,Jade,Mathematics,100,,,
$ awk '/Jade/ {print "****"; print $0}' ./students.csv
****
5553,Jade,English,73,,,
****
5553,Jade,Mathematics,100,,,
$ awk '/Jade/ {print "****"; print $2}' ./students.csv
****
****
$ awk -F, '/Jade/ {print "****"; print $2}' ./students.csv
****
Jade
****
Jade
$ awk -F, '/Jade/ {print "****"; print $3}' ./students.csv
****
English
****
Mathematics
$ awk -F, '/Jade/ {print "****"; print $4}' ./students.csv
****
73
****
100
$ awk -F, '/Jade/ {print $4}' ./students.csv
73
100
$ awk -F, '{print $4}' ./students.csv
Score
96
77
61
97
87
90
81
63
96
73
71
77
65
87
85
70
70
69
89
79
75
62
69
87
86
100
98
84
100
89
91
99
77
62
74
93
77
88
$ awk -F, '/Mathematics/' ./students.csv
5329,Alix,Mathematics,79,,,
3250,Bronagh,Mathematics,75,,,
5888,Charlie,Mathematics,62,,,
5978,Dean,Mathematics,69,,,
2264,Edmund,Mathematics,87,,,
2307,Fiona,Mathematics,86,,,
1835,Gerry,Mathematics,100,,,
2758,Hettie,Mathematics,98,,,
5787,Iain,Mathematics,84,,,
5553,Jade,Mathematics,100,,,
6139,Kane,Mathematics,89,,,
9301,Lauren,Mathematics,91,,,
9856,Michael,Mathematics,99,,,
6691,Nora,Mathematics,77,,,
4724,Oscar,Mathematics,62,,,
7793,Paula,Mathematics,74,,,
8944,Quentin,Mathematics,93,,,
6112,Rose,Mathematics,77,,,
4145,Steve,Mathematics,88,,,
$ awk -F, '$3 ~ /Mathematics/' ./students.csv
5329,Alix,Mathematics,79,,,
3250,Bronagh,Mathematics,75,,,
5888,Charlie,Mathematics,62,,,
5978,Dean,Mathematics,69,,,
2264,Edmund,Mathematics,87,,,
2307,Fiona,Mathematics,86,,,
1835,Gerry,Mathematics,100,,,
2758,Hettie,Mathematics,98,,,
5787,Iain,Mathematics,84,,,
5553,Jade,Mathematics,100,,,
6139,Kane,Mathematics,89,,,
9301,Lauren,Mathematics,91,,,
9856,Michael,Mathematics,99,,,
6691,Nora,Mathematics,77,,,
4724,Oscar,Mathematics,62,,,
7793,Paula,Mathematics,74,,,
8944,Quentin,Mathematics,93,,,
6112,Rose,Mathematics,77,,,
4145,Steve,Mathematics,88,,,
$ awk -F, '$3 ~ /Mathematics/ && $4 < 70' ./students.csv
5888,Charlie,Mathematics,62,,,
5978,Dean,Mathematics,69,,,
4724,Oscar,Mathematics,62,,,
$ awk -F, '$3 ~ /Mathematics/ && $4 < 70 {tally += 1} END {print tally}' ./students.csv
3
$ awk -F, '$3 ~ /Mathematics/ && $4 < 70 {print $2; tally += 1} END {print tally}' ./students.csv
Charlie
Dean
Oscar
3
$ awk -F, '$3 ~ /Mathematics/ && $4 < 70 {print $2; tally += 1} END {print "total... %tally"}' ./students.csv
Charlie
Dean
Oscar
total... %tally
$ awk -F, '$3 ~ /Mathematics/ && $4 < 70 {print $2; tally += 1} END {print "total... $tally"}' ./students.csv
Charlie
Dean
Oscar
total... $tally
$ awk -F, '$3 ~ /Mathematics/ && $4 < 70 {print $2; tally += 1} END {print total... $tally}' ./students.csv
awk: syntax error at source line 1
context is
$3 ~ /Mathematics/ && $4 < 70 {print $2; tally += 1} END {print >>> total... <<< $tally}
awk: illegal statement at source line 1
$ awk -F, '$3 ~ /Mathematics/ && $4 < 70 {print $2; tally += 1} END {print "total..."; print tally}' ./students.csv
Charlie
Dean
Oscar
total...
3
ID Name Subject Score
5329 Alix English 96
3250 Bronagh English 77
5888 Charlie English 61
5978 Dean English 97
2264 Edmund English 87
2307 Fiona English 90
1835 Gerry English 81
2758 Hettie English 63
5787 Iain English 96
5553 Jade English 73
6139 Kane English 71
9301 Lauren English 77
9856 Michael English 65
6691 Nora English 87
4724 Oscar English 85
7793 Paula English 70
8944 Quentin English 70
6112 Rose English 69
4145 Steve English 89
5329 Alix Mathematics 79
3250 Bronagh Mathematics 75
5888 Charlie Mathematics 62
5978 Dean Mathematics 69
2264 Edmund Mathematics 87
2307 Fiona Mathematics 86
1835 Gerry Mathematics 100
2758 Hettie Mathematics 98
5787 Iain Mathematics 84
5553 Jade Mathematics 100
6139 Kane Mathematics 89
9301 Lauren Mathematics 91
9856 Michael Mathematics 99
6691 Nora Mathematics 77
4724 Oscar Mathematics 62
7793 Paula Mathematics 74
8944 Quentin Mathematics 93
6112 Rose Mathematics 77
4145 Steve Mathematics 88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment