Skip to content

Add library of helper functions for creating/processing CSV files #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions lib.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

# TODO: add tests for these

# Set fields from array a according to the order in OFIELDS, which must have
# field numbers as keys (from 1 to N) and field names as values, for example
# OFIELDS[1] = "name"; OFIELDS[2] = "age".
function setfields(a, i) {
for (i=1; i in OFIELDS; i++) {
$i = a[OFIELDS[i]]
}
NF = i-1
}

# Call setfields(a) and then print the current row.
function printfields(a) {
setfields(a)
print
}

# Print the header (field names) from OFIELDS
function printheader( i) {
for (i=1; i in OFIELDS; i++) {
$i = OFIELDS[i]
}
NF = i-1
print
}

# Delete the nth field from $0. If num is given, delete num fields starting
# from the nth field.
function delfield(n, num, i) {
if (n < 1 || n > NF || num < 0) {
$1 = $1 # ensure $0 gets rewritten
return
}
if (num == 0) {
num = 1
}
if (num > NF-n+1) {
num = NF-n+1
}
for (i=n; i<=NF-num; i++) {
$i = $(i+num)
}
NF -= num
}

# Insert a new empty field just before the nth field in $0. If num is given,
# insert num empty fields just before the nth field.
function insfield(n, num, i) {
if (n < 1 || num < 0) {
$1 = $1 # ensure $0 gets rewritten
return
}
if (num == 0) {
num = 1
}
for (i=NF; i>=n; i--) {
$(i+num) = $i
}
for (i=n; i<n+num; i++) {
$i = ""
}
}

# Return the number of the given named field, or 0 if there's no field with
# that name. Only works in -H/header mode.
function fieldnum(name, i) {
for (i=1; i in FIELDS; i++) {
if (FIELDS[i] == name) {
return i
}
}
return 0
}