-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65f84ce
commit 669327f
Showing
7 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#!/usr/bin/env mshell-go | ||
|
||
# 1. Print the total nubmer of input lines: | ||
# END { print NR } | ||
stdin lines len wl | ||
|
||
# 2. Print the 10th input line: | ||
# NR == 10 | ||
stdin lines :10: wl | ||
|
||
# 3. Print the last field of every input line: | ||
# { print $NF } | ||
stdin lines (" " split :-1: wl) each | ||
|
||
# 4. Print the last field of the last input line: | ||
# { field = $NF } | ||
# END { print field } | ||
stdin lines :-1: " " split :-1: wl | ||
|
||
# 5. Print every input line with more than four fields | ||
# NF > 4 | ||
stdin lines (" " split) map (len 4 >) filter unlines wl | ||
|
||
# 6. Print every input line in which the last field is more than 4 | ||
# $NF > 4 | ||
stdin lines (" " split) map (:-1: toFloat 4 >) filter unlines wl | ||
|
||
# 7. Print the total number of fields in all input lines | ||
# { nf = nf + $NF } | ||
# END { print nf } | ||
stdin lines (" " split len) map sum wl | ||
|
||
# 8. Print the total number of lines that contain 'Beth' | ||
# /Beth/ { nlines = nlines + 1 } | ||
# END { print nlines } | ||
stdin lines ("Beth" in) filter len wl | ||
|
||
# 9. Print the largest first field and the line that contains it (assumes some $1 is positive): | ||
# $1 > max { max = $1; line = $0 } | ||
# END { print max, line } | ||
|
||
0 @max | ||
stdin lines | ||
# line first | ||
(dup " " split :1: toFloat [(dup max! >) (@max @line)] if) | ||
each max! w " " w line! w | ||
|
||
# 10. Print every line that has at least one field | ||
# NF > 0 | ||
stdin lines (len 0 >) filter (wl) each | ||
|
||
# 11. Print every line longer than 80 characters | ||
# length($0) > 80 | ||
stdin lines (len 80 >) filter (wl) each | ||
|
||
# 12. Print the number of fields in every line followed by the line itself | ||
# { print NF, $0 } | ||
stdin lines (dup " " split len w " " w wl) each | ||
|
||
# 13. Print the first two fields in opposite order, of every line | ||
# { print $2, $1 } | ||
stdin lines (" " split :1: :2: w w) each | ||
|
||
# 14. Exchange the first two fields of every line and then print the line | ||
# { temp = $1; $1 = $2; $2 = temp; print } | ||
# Need a way to write value into an index. | ||
# stdin lines (dup " " split :1: :2: swap w w) each | ||
|
||
# 15. Print every line with the first field replaced by the line number | ||
# { $1 = NR; print } | ||
# Need a way to write value into an index. | ||
# stdin lines (dup w 1 w w) each | ||
|
||
# 16. Print every line after erasing the second field | ||
# { $2 = ""; print } | ||
# Need a way to delete at an index. | ||
|
||
# 17. Print in reverse order the fields of every line | ||
# { for (i = NF; i > 0; i = i - 1) printf "%s ", $i | ||
# printf "\n" | ||
# } | ||
stdin lines (" " split reverse " " join wl) each | ||
|
||
# 18. Print the sums of the fields of every line | ||
# { sum = 0 | ||
# for (i = 1; i <= NF; i = i + 1) sum = sum + $i | ||
# print sum | ||
# } | ||
=> (" " split (toFloat) map sum wl) each | ||
|
||
# 19. Add up all fields in all lines and print the sum | ||
# { for (i = 1; i <= NF; i = i + 1) sum = sum + $i } | ||
# END { print sum } | ||
stdin lines (" " split (toFloat) map sum) map sum wl | ||
|
||
# 20. Print every line after replacing each field by its absolute value | ||
# { for (i = 1; i <= NF; i = i + 1) $i = ($i < 0) ? -$i : $i; print } | ||
stdin lines (" " split (toFloat abs) map " " join wl) each |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
END { print NR } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.. len wl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NR == 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.. :10: wl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Beth 4.00 0 | ||
Dan 3.75 0 | ||
Kathy 4.00 10 | ||
Mark 5.00 20 | ||
Mary 5.50 22 | ||
Susie 4.25 18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters