Skip to content

Commit

Permalink
add more awk samples
Browse files Browse the repository at this point in the history
  • Loading branch information
edomin committed Jul 20, 2020
1 parent d96e409 commit 80999d1
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Samples:
awk. I found some exercises in the network and trying to complete it.
Directory structure:
- awk/numbers - simple exercises for processing 2d array of integer numbers.
- awk/people - sample of parsing people data file
- awk/exercism - exercises from
[exercism.io](https://exercism.io/tracks/bash/exercises) adapted from bash to
awk
* [**inih**](https://github.com/edomin/samples/tree/master/inih) -
attempt to inplement library-like API for working with ini files using inih
library. Sample consist: Ini structure with sections containing key-value data,
Expand Down
22 changes: 22 additions & 0 deletions awk/exercism/acronym.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/awk -f

# Source: https://exercism.io/tracks/bash/exercises/acronym

# Help generate some jargon by writing a program that converts a long name like
# Portable Network Graphics to its acronym (PNG).

BEGIN {
if (ARGC == 1) {
print "Usage:\n./acronym.awk <word1> <word2> ...";
exit 1;
}

acronym = "";

for (i = 1; i < ARGC; i++) {
split(ARGV[i], array, "");
acronym = acronym array[1];
}

print toupper(acronym);
}
37 changes: 37 additions & 0 deletions awk/exercism/armstrong_numbers.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/awk -f

# Source: https://exercism.io/tracks/bash/exercises/armstrong-numbers

# An Armstrong number is a number that is the sum of its own digits each raised
# to the power of the number of digits.
# For example:
# 9 is an Armstrong number, because 9 = 9^1 = 9
# 10 is not an Armstrong number, because 10 != 1^2 + 0^2 = 1
# 153 is an Armstrong number, because: 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27
# = 153
# 154 is not an Armstrong number, because: 154 != 1^3 + 5^3 + 4^3 = 1 + 125
# + 64 = 190
#
# Write some code to determine whether a number is an Armstrong number.

BEGIN {
if (ARGC == 1) {
print "Usage:\n./armstrong_numbers.awk <number>";
exit 1;
}

split(ARGV[1], digits, "");
sum = 0;
len = length(ARGV[1]);

for (i = 1; i <= len; i++) {
split(ARGV[i], array, "");

sum += digits[i] ^ len;
}

if (ARGV[1] == sum)
print "Armstrong number";
else
print "Simple number";
}
11 changes: 11 additions & 0 deletions awk/exercism/error_handling.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/awk -f

# Source: https://exercism.io/tracks/bash/exercises/error-handling

BEGIN {
if (ARGC <= 1) {
print "Usage:\n./error_handling.awk <person>";
exit 1;
}
print "Hello, " ARGV[1];
}
52 changes: 52 additions & 0 deletions awk/exercism/hamming.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/awk -f

# Source: https://exercism.io/tracks/bash/exercises/hamming

# Calculate the Hamming Distance between two DNA strands.
# Your body is made up of cells that contain DNA. Those cells regularly wear
# out and need replacing, which they achieve by dividing into daughter cells.
# In fact, the average human body experiences about 10 quadrillion cell
# divisions in a lifetime!
#
# When cells divide, their DNA replicates too. Sometimes during this process
# mistakes happen and single pieces of DNA get encoded with the incorrect
# information. If we compare two strands of DNA and count the differences
# between them we can see how many mistakes occurred. This is known as the
# "Hamming Distance".
#
# We read DNA using the letters C,A,G and T. Two strands might look like this:
# GAGCCTACTAACGGGAT
# CATCGTAATGACGGCCT
# ^ ^ ^ ^ ^ ^^
# They have 7 differences, and therefore the Hamming Distance is 7.

# Implementation notes
# The Hamming distance is only defined for sequences of equal length, so an
# attempt to calculate it between sequences of different lengths should not
# work.

BEGIN {
if (ARGC <= 2) {
print "Usage:\n./hamming.awk <dna1> <dna2>";
exit 1;
}

len1 = length(ARGV[1]);
len2 = length(ARGV[2]);

if (len1 != len2) {
print "DNAs' lengths are not equal."
exit 2;
}

split(ARGV[1], dna1, "");
split(ARGV[2], dna2, "");

hamming = 0;
for (i = 1; i <= len1; i++) {
if (dna1[i] != dna2[i])
hamming++;
}

print "Hamming distance: " hamming;
}
5 changes: 5 additions & 0 deletions awk/exercism/hello_world.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/awk -f

BEGIN {
print "Hello World"
}
34 changes: 34 additions & 0 deletions awk/exercism/raindrops.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/awk -f

# Source: https://exercism.io/tracks/bash/exercises/raindrops

# Your task is to convert a number into a string that contains raindrop sounds
# corresponding to certain potential factors. A factor is a number that evenly
# divides into another number, leaving no remainder. The simplest way to test
# if a one number is a factor of another is to use the modulo operation.
# The rules of raindrops are that if a given number:
# has 3 as a factor, add 'Pling' to the result.
# has 5 as a factor, add 'Plang' to the result.
# has 7 as a factor, add 'Plong' to the result.
# does not have any of 3, 5, or 7 as a factor, the result should be the
# digits of the number.

BEGIN {
if (ARGC <= 1) {
print "Usage:\n./raindrops.awk <number>";
exit 1;
}

sound = "";

if (ARGV[1] % 3 == 0)
sound = sound "Pling";
if (ARGV[1] % 5 == 0)
sound = sound "Plang";
if (ARGV[1] % 7 == 0)
sound = sound "Plong";
if (sound == "")
sound = ARGV[1];

print sound;
}
19 changes: 19 additions & 0 deletions awk/exercism/two_fer.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/awk -f

# Source: https://exercism.io/tracks/bash/exercises/two-fer

# Two-fer or 2-fer is short for two for one. One for you and one for me.
# Given a name, return a string with the message:
# One for name, one for me.
# Where "name" is the given name.
# However, if the name is missing, return the string:
# One for you, one for me.

BEGIN {
if (ARGC > 1)
you = ARGV[1];
else
you = "you";

print "One for " you ", one for me.";
}
25 changes: 25 additions & 0 deletions awk/people/people.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Exercise source: https://linux.die.net/Bash-Beginners-Guide/sect_06_05.html

# For the this exercise, your input is lines in the following form:
# Username:Firstname:Lastname:Telephone number
#
# Make an awk script that will convert such a line to an LDAP record in this
# format:
# dn: uid=Username, dc=example, dc=com
# cn: Firstname Lastname
# sn: Lastname
# telephoneNumber: Telephone number
#
# Create a file containing a couple of test records and check.

BEGIN {
FS = ":";
}

{
print "dn: uid=" $1 ", dc=example, dc=com";
print "cn: " $2 " " $3;
print "sn: " $3;
print "telephoneNumber: " $4;
print "";
}
10 changes: 10 additions & 0 deletions awk/people/people.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
damian:Barbara:Collins:929-233-5128
danika_toy:Kathryn:Reed:781-492-9464
vida:Robert:Jones:626-641-1456
jordan:Debbie:Phillips:646-515-9319
Rosalveryb:Danny:Major:601-804-3755
selfpropulsion:Katie:Dart:504-442-3536
sitsang:Brett:Rivas:870-751-8983
felipa_sta1988:Jerry:Beaver:432-249-8790
alvis:Julie:Carter:334-524-9935
JBU21219:Shane:Ogg:208-680-9156

0 comments on commit 80999d1

Please sign in to comment.