Skip to content

Commit

Permalink
Add awk examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Nov 14, 2024
1 parent 65f84ce commit 669327f
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 2 deletions.
98 changes: 98 additions & 0 deletions examples/awk.msh
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
1 change: 1 addition & 0 deletions examples/awk/1.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
END { print NR }
1 change: 1 addition & 0 deletions examples/awk/1.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. len wl
1 change: 1 addition & 0 deletions examples/awk/2.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NR == 10
1 change: 1 addition & 0 deletions examples/awk/2.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. :10: wl
6 changes: 6 additions & 0 deletions examples/awk/emp.data
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
5 changes: 3 additions & 2 deletions lib/std.msh
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def map
drop drop map-accum!
end

# .. (-- str)
def .. stdin end

# .. (-- list[str]), equals kinda looks like lines.
def .. stdin lines end

# tt = Tab separated Table (-- list[list[str]])
def tt stdin lines (" " split) map end
Expand Down

0 comments on commit 669327f

Please sign in to comment.