Skip to content

Commit

Permalink
Added another AWK attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
mirao committed Jun 15, 2016
1 parent eca6a6d commit 34d6562
Show file tree
Hide file tree
Showing 21 changed files with 244 additions and 10 deletions.
Binary file added .vscode/.browse.VC.db
Binary file not shown.
Binary file added .vscode/.browse.VC.db-shm
Binary file not shown.
Binary file added .vscode/.browse.VC.db-wal
Binary file not shown.
1 change: 1 addition & 0 deletions addresses.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Robbins,,"1234 A Pretty Street, NE",MyTown,MyState,12345-6789,USA
12 changes: 12 additions & 0 deletions addrs.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# addrs.awk --- simple mailing list program

# Records are separated by blank lines.
# Each line is one field.
BEGIN { RS = "" ; FS = "\n" }

{
print "Name is:", $1
print "Address is:", $2
print "City and State are:", $3
print ""
}
10 changes: 0 additions & 10 deletions awkprof.out

This file was deleted.

7 changes: 7 additions & 0 deletions changeFSonthefly.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# run "awk -f changeFSonthefly who.txt"
NR < 4 {FIELDWIDTHS = "1 1"}
NR >= 4 {FS = FS}
{
$0 = $0; # it's important to reload $0, otherwise change in FS is not performed
print $1 ";" $2 ";" PROCINFO["FS"];
}
6 changes: 6 additions & 0 deletions comments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Zacatek
/*
* ahoj
* caues
*/
Konec
16 changes: 16 additions & 0 deletions fpat.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
BEGIN {
FPAT = "([^,]*)|(\"[^\"]+\")"
}

{
print "NF = ", NF
for (i = 1; i <= NF; i++) {

# if (substr($i, 1, 1) == "\"") {
# len = length($i)
# $i = substr($i, 2, len - 2) # Get text within the two quotes
# }
$i = gensub(/^"(.+)"$/, "\\1", 1, $i) # remove double quotes
printf("$%d = <%s>\n", i, $i)
}
}
16 changes: 16 additions & 0 deletions getline.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# copies input to output
# shows random line from /etc/passwd if line begins with @random
BEGIN {
srand()
passwd = "/etc/passwd"
("wc -l " passwd) | getline count # number of lines in passwd file
}
{
if ($1 == "@random") {
line = int(count * rand()) + 1 # generate random line number
tmp = "tail -" line " " passwd "|head -1" # get random line
tmp | getline
close(tmp)
}
print
}
6 changes: 6 additions & 0 deletions getline.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@random
bar
baz
@random
fred
joe
16 changes: 16 additions & 0 deletions pipeline.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# gawk profile, created Tue Jun 14 20:01:00 2016

# BEGIN rule(s)

BEGIN {
cmd = "sort -n"
ret = "12;111;Ahoj;čaues;nazdarek;drstkova"
num = split(ret, pole, ";")
for (id in pole)
print(pole[id]) |& cmd
close(cmd, "to")
while ((cmd |& getline) > 0)
print
close(cmd)
}

6 changes: 6 additions & 0 deletions print_content_of_file_A.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# print all lines of input files
# long variant
BEGIN {
while (getline)
print $0
}
3 changes: 3 additions & 0 deletions print_content_of_file_B.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# print all lines of input files
# simple variant
1
20 changes: 20 additions & 0 deletions remove_comments.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>

int main()
{
// Single line comment in c source code

printf("Writing comments is very useful.\n"); /* one more */ /* OK */
printf("Nazdarek"); /* jiste */
/* wov efekt */ printf("Na ja"); /* budeme
hola */ printf("Jiste");
/*
* Multi line comment syntax
* Comments help us to understand code later easily.
* Will you write comments while developing programs ?
*/

printf("Good luck c programmer number %d.\n", 1);

return 0;
}
29 changes: 29 additions & 0 deletions remove_comments_A.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Remove text between /* and */, inclusive
{
if ((i = index($0, "/*")) != 0) {
out = substr($0, 1, i - 1) # leading part of the string
rest = substr($0, i + 2) # ... */ ...
j = index(rest, "*/") # is */ in trailing part?
if (j > 0) {
rest = substr(rest, j + 2) # remove comment
} else {
while (j == 0) {
# get more text
if (getline <= 0) {
print("unexpected EOF or error:", ERRNO) > "/dev/stderr"
exit
}
# build up the line using string concatenation
rest = rest $0
j = index(rest, "*/") # is */ in trailing part?
if (j != 0) {
rest = substr(rest, j + 2)
break
}
}
}
# build up the output line using string concatenation
$0 = out rest
}
print $0
}
43 changes: 43 additions & 0 deletions remove_comments_B.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Remove text between /* and */, inclusive
# It removes even multiple occurences on line

BEGIN {
findleft = 1
leftmark = "/*"
rightmark = "*/"
}

{
leftpart = ""
rightpart = $0
nocomment = ""
while (!findleft || (idleft = index(rightpart, leftmark)) > 0) {
if (findleft) {
leftpart = substr(rightpart, 1, idleft - 1) # part before comment
commentpart = substr(rightpart, idleft + 2) # part after /*
}
else # searching for */ that was not found on previous line
commentpart = rightpart

nocomment = nocomment leftpart # prepare the part without comments

idright = index(commentpart, rightmark) # index of */
if (idright > 0) {
rightpart = substr(commentpart, idright + 2) # part after */
findleft = 1 # */ found, next time find /*
}
else { # */ not found
findleft = 0 # find */ on the next line
if (nocomment != "")
print nocomment
next
}
}

# /* not found
nocomment = nocomment rightpart
findleft = 1 # find /* on the next line

if (nocomment != "")
print nocomment
}
18 changes: 18 additions & 0 deletions remove_comments_C.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Remove text between /* and */, inclusive
# It removes even multiple occurences on line

BEGIN {
RS="\f"
}

{
rid = 1
orig = $0
while ((lid = index($orig, "/*")) > 0) {
comment = substr($orig, lid + 2)
code = code substr($orig, 1, lid - 1)
rid = index(comment, "*/") + 2
$orig = substr($orig, lid + rid + 1)
}
print code $orig
}
20 changes: 20 additions & 0 deletions seq.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
15 changes: 15 additions & 0 deletions who.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BEGIN { FIELDWIDTHS = "9 6 10 6 7 7 35" }
NR > 2 {
idle = $4
sub(/^ +/, "", idle) # strip leading spaces
if (idle == "")
idle = 0
if (idle ~ /:/) {
split(idle, t, ":")
idle = t[1] * 60 + t[2]
}
if (idle ~ /days/)
idle *= 24 * 60 * 60

print $1, $2, idle
}
10 changes: 10 additions & 0 deletions who.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
10:06pm up 21 days, 14:04, 23 users
User tty login idle JCPU PCPU what
hzuo ttyV0 8:58pm 9 5 vi p24.tex
hzang ttyV3 6:37pm 50 -csh
eklye ttyV5 9:53pm 7 1 em thes.tex
dportein ttyV6 8:17pm 1:47 -csh
gierd ttyD3 10:00pm 1 elm
dave ttyD4 9:47pm 4 4 w
brent ttyp0 26Jun91 4:46 26:46 4:41 bash
dave ttyq4 26Jun9115days 46 46 wnewmail

0 comments on commit 34d6562

Please sign in to comment.