aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-097/abigail/README.md1
-rw-r--r--challenge-097/abigail/awk/ch-1.awk53
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md
index bd64d4cbbe..6d03a861aa 100644
--- a/challenge-097/abigail/README.md
+++ b/challenge-097/abigail/README.md
@@ -24,6 +24,7 @@ We will be reading the plain text from STDIN, and use an option (-s)
to indicate the left shift.
### Solutions
+* [AWK](awk/ch-1.awk)
* [Bash](bash/ch-1.sh)
* [Node.js](node/ch-1.js)
* [Perl](perl/ch-1.pl)
diff --git a/challenge-097/abigail/awk/ch-1.awk b/challenge-097/abigail/awk/ch-1.awk
new file mode 100644
index 0000000000..a33507ee33
--- /dev/null
+++ b/challenge-097/abigail/awk/ch-1.awk
@@ -0,0 +1,53 @@
+#!/usr/bin/awk
+
+#
+# See ../README.md
+#
+
+#
+# Run as: awk -f ch-1.awk -s SHIFT < input-file
+#
+
+BEGIN {
+ NR_OF_LETTERS = 26
+ ORD_A = 65
+
+ #
+ # Create a letter to number mapping
+ #
+ for (i = ORD_A; i < ORD_A + NR_OF_LETTERS; i ++) {
+ t = sprintf ("%c", i)
+ ord [t] = i
+ }
+
+ #
+ # Parse command line
+ #
+ for (i = 1; i < ARGC; i ++) {
+ if (ARGV [i] == "-s") {
+ shift = ARGV [i + 1]
+ }
+ }
+ ARGC = 0
+}
+
+{
+ split($0, letters, "")
+ out = ""
+ #
+ # Iterate over the individual letters, shifting capital letters
+ #
+ for (i = 1; i <= length (letters); i ++) {
+ char = letters [i]
+ if (ord [char]) {
+ n = ord [char] - shift
+ if (n < ORD_A) {
+ n = n + NR_OF_LETTERS
+ }
+ char = sprintf ("%c", n)
+ }
+ out = out char
+ }
+ print out
+}
+