Remove awk and add x86 history.

c-declarations
Joshua Potter 2024-03-20 11:33:13 -06:00
parent 74e7b8df0c
commit 3b6d9d89c5
10 changed files with 280 additions and 851 deletions

View File

@ -179,7 +179,7 @@
"c/strings.md": "aba6e449906d05aee98e3e536eb43742",
"logic/truth-tables.md": "7892ceaa416c9a65acc79ca1e6ff778f",
"logic/short-circuit.md": "e088e62ead26779f9a51dfd1caeeb9d4",
"logic/boolean-algebra.md": "00dc2cdc93d6b0e0e60c975ba87f8633",
"logic/boolean-algebra.md": "ef381cb4a991d4d36fa44663a85e7927",
"_journal/2024-02-13.md": "6242ed4fecabf95df6b45d892fee8eb0",
"_journal/2024-02/2024-02-12.md": "618c0035a69b48227119379236a02f44",
"binary/shifts.md": "9bbeef29e98c3ab521f44b87528cf5c2",
@ -209,12 +209,12 @@
"combinatorics/inclusion-exclusion.md": "202a60120d451676d44df4d0be30a45a",
"_journal/2024-02-21.md": "b9d944ecebe625da5dd72aeea6a916a2",
"_journal/2024-02/2024-02-20.md": "af2ef10727726200c4defe2eafc7d841",
"algebra/radices.md": "4ffc8525f2b877c0d17e3359870797fe",
"algebra/radices.md": "03b8452798a6fd4ec5af526371309c4f",
"_journal/2024-02-22.md": "e01f1d4bd2f7ac2a667cdfd500885a2a",
"_journal/2024-02/2024-02-21.md": "f423137ae550eb958378750d1f5e98c7",
"_journal/2024-02-23.md": "219ce9ad15a8733edd476c97628b71fd",
"_journal/2024-02/2024-02-22.md": "312e55d57868026f6e80f7989a889c2b",
"c17/strings.md": "55456a1d5b854d5d4f87184f03461db9",
"c17/strings.md": "b9d2efd54b9a06f50042fb2714dedbf4",
"c17/index.md": "78576ee41d0185df82c59999142f4edb",
"c17/escape-sequences.md": "a8b99070336878b4e8c11e9e4525a500",
"c17/declarations.md": "cec6866dff8ad160467df62cfceb6872",
@ -289,11 +289,13 @@
"awk/variables.md": "e40a20545358228319f789243d8b9f77",
"awk/regexp.md": "4ce38103575a5321a1503b28e1d714dd",
"awk/index.md": "257738d2d864933fb4bd21e8609c525d",
"_journal/2024-03-20.md": "2e81bab565b347d045ffee4123009bbf",
"_journal/2024-03-20.md": "9f6ff3e38774f6a90e0dc606b201303d",
"_journal/2024-03/2024-03-19.md": "a24995a82bcebcc2b2aa5815a19a8af7",
"posix/awk/variables.md": "cc7e912c7fbda552d27e2064d84d15a5",
"posix/awk/regexp.md": "46ceac3d5225ea3f3a375d74c2e2e0e9",
"posix/awk/index.md": "cac4a1db94f9fc39c5e63ff6994b76aa"
"posix/awk/index.md": "cac4a1db94f9fc39c5e63ff6994b76aa",
"assembly/x86.md": "ff0f19c990e16008caa34dd51b2693b7",
"assembly/index.md": "0cd9daf2c60c7181b6aec9aaf62a13ba"
},
"fields_dict": {
"Basic": [

View File

@ -11,8 +11,10 @@ title: "2024-03-20"
- [ ] Log Work Hours (Max 3 hours)
* Purged some Anki flashcards with tags: `lua`, `cncf`. In general, want to avoid on this next pass including any sort of flashcards around languages outside of C and:
* `awk`
* Bash
* Lean
* Nix
* Assembly
* Made distinctions in flashcards between POSIX `awk` features and GNU `awk` features. Only a few exceptions that may have been misleading so far (GNU's implementation allowing `FS` to be null and `RS` to be a regexp).
* Made distinctions in flashcards between POSIX `awk` features and GNU `awk` features. Only a few exceptions that may have been misleading so far (GNU's implementation allowing `FS` to be null and `RS` to be a regexp).
* Actually, going further, just removing this altogether. I'll continue reading about `awk` but I'm not interested in translating into flashcards unless it turns out I really start using it heavily.
* Read Chapter 5 "Printing" in "GAWK: Effective AWK Programming".
* Notes on [[x86]] history.

View File

@ -126,7 +126,7 @@ Basic
How does C denote a hexadecimal numeric constant?
Back: With `0x` or `0X`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary::hex c
Tags: binary::hex c17
<!--ID: 1707432641567-->
END%%

7
notes/assembly/index.md Normal file
View File

@ -0,0 +1,7 @@
---
title: Assembly
TARGET DECK: Obsidian::STEM
FILE TAGS: asm
tags:
- asm
---

244
notes/assembly/x86.md Normal file
View File

@ -0,0 +1,244 @@
---
title: x86
TARGET DECK: Obsidian::STEM
FILE TAGS: asm::x86
tags:
- asm
- x86
---
## Overview
x86 refers to the architecture first introduced by Intel with their 8086 processor (1978). Intel's i386 (1985), originally named the 80386, expanded the architecture to 32-bits. AMD's Opteron (2003) expanded it again to 64-bits.
%%ANKI
Basic
Which company invented x86?
Back: Intel.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846934-->
END%%
%%ANKI
Basic
Which two companies build x86-compatible processors?
Back: Intel and AMD.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846937-->
END%%
%%ANKI
Basic
Which processor gave rise to the x86 ISA?
Back: Intel 8086.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846939-->
END%%
%%ANKI
Basic
The Intel 8086 was released in which decade?
Back: The 1970s.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846942-->
END%%
%%ANKI
Basic
Which processor first expanded the x86 ISA to 32-bit?
Back: Intel i386
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846944-->
END%%
%%ANKI
Basic
What is IA-32?
Back: The 32-bit version of the x86 ISA.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846946-->
END%%
%%ANKI
Basic
What is IA-32 an acronym for?
Back: **I**ntel **A**rchitecture **32**-bit.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846948-->
END%%
%%ANKI
Cloze
The {80386} was the original name for the {i386} processor.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846950-->
END%%
%%ANKI
Cloze
The 8086 is {1:16}-bit whereas the i386 is {1:32}-bit.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846952-->
END%%
%%ANKI
Basic
What is IA-64?
Back: The 64-bit implementation of the Itanium ISA.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846955-->
END%%
%%ANKI
Basic
What is Intel 64?
Back: Another name for x86-64.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846957-->
END%%
%%ANKI
Cloze
IA-32 is to the {1:x86} ISA whereas IA-64 is to the {1:Itanium} ISA.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846960-->
END%%
%%ANKI
Basic
What distinguishes x86-64 from IA-64?
Back: These refer to incompatible ISAs.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846964-->
END%%
%%ANKI
Basic
What distinguishes x86-64 from IA-32?
Back: The former is the 64 bit extension to the latter.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846967-->
END%%
%%ANKI
Basic
What is x86-64?
Back: The 64-bit version of the x86 ISA.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846969-->
END%%
%%ANKI
Basic
Why is the x86 processor line named the way it is?
Back: The original processors making up the line were named `<SOMETHING>86`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846971-->
END%%
%%ANKI
Basic
In which decade was the 80386 released?
Back: The 1980s.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846973-->
END%%
%%ANKI
Basic
What was the first x86 processor capable of supporting 16-bit?
Back: Intel 8086
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846975-->
END%%
%%ANKI
Basic
What was the first x86 processor capable of supporting 32-bit?
Back: Intel i386
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846977-->
END%%
%%ANKI
Basic
What was the first x86 processor capable of supporting 64-bit?
Back: AMD Opteron
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846980-->
END%%
%%ANKI
Basic
Which company released the first 16-bit version of the x86 architecture?
Back: Intel.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846983-->
END%%
%%ANKI
Basic
Which company released the first 32-bit version of the x86 architecture?
Back: Intel.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846985-->
END%%
%%ANKI
Basic
Which company released the first 64-bit version of the x86 architecture?
Back: AMD.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846987-->
END%%
%%ANKI
Basic
In which decade was the Opteron released?
Back: The 2000s.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846989-->
END%%
%%ANKI
Basic
What is the significance of the 8086 in x86's history?
Back: This processor introduced the x86 architecture.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846991-->
END%%
%%ANKI
Basic
What is the significance of the i386 in x86's history?
Back: This processor introduced the 32-bit version of the x86 architecture.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846994-->
END%%
%%ANKI
Basic
What is the significance of the Opteron in x86's history?
Back: This processor introduced the 64-bit version of the x86 architecture.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846996-->
END%%
%%ANKI
Cloze
The 8086 is {1:16}-bit whereas the Opteron is {1:64}-bit.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955846999-->
END%%
%%ANKI
Basic
What is AMD64?
Back: Another name for x86-64.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1710955847001-->
END%%
## References
* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.

View File

@ -954,7 +954,6 @@ C has a standard for processing different escape sequences. Many languages built
* `\ooo`: Consists of one to three octal digits.
* [[bash/index|Bash]] supports this sequence as `$'\ooo'`.
* [[posix/awk/index|awk]] supports this sequence directly.
%%ANKI
Basic
@ -983,7 +982,6 @@ END%%
* `\xhh`: Consists of one or more [[radices#Hexadecimal|hexadecimal]] digits. The `x` prefix is required to distinguish from octal escape sequences.
* [[bash/index|Bash]] supports this sequence as `$'\xhh'`. One or two digits is supported.
* POSIX [[posix/awk/index|awk]] does not support this sequence.
%%ANKI
Basic
@ -1011,14 +1009,13 @@ END%%
* `\uhhhh`: Introduced in C11 to represent Unicode code points. *Must* have exactly four hexadecimal characters specified with `0` leading padding if necessary.
* [[bash/index|Bash]] supports this sequence as `$'uhhhh'`. One to four hex digits is supported.
* POSIX [[posix/awk/index|awk]] does not support this sequence.
%%ANKI
Basic
What two ways are C escape sequences for unicode denoted?
Back: As `\uhhhh` or `\Uhhhhhhhh`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
Tags: unicode
Tags: encoding::unicode
<!--ID: 1706975891832-->
END%%
@ -1027,7 +1024,7 @@ Basic
In C, `\u` allows specifying how many hexadecimal digits?
Back: Exactly four.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
Tags: unicode
Tags: encoding::unicode
<!--ID: 1706975891835-->
END%%
@ -1036,7 +1033,7 @@ Basic
In what standard were C's `\u` and `\U` escape sequences introduced?
Back: C11.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
Tags: unicode
Tags: encoding::unicode
<!--ID: 1706975891839-->
END%%
@ -1047,7 +1044,7 @@ Basic
In C, `\U` allows specifying how many hexadecimal digits?
Back: Exactly eight.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
Tags: unicode
Tags: encoding::unicode
<!--ID: 1706975891843-->
END%%
@ -1056,7 +1053,7 @@ Basic
Why does C have both `\u` and `\U`?
Back: `\U` accommodates for larger code point values.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
Tags: unicode
Tags: encoding::unicode
<!--ID: 1706976705750-->
END%%

View File

@ -93,7 +93,7 @@ Basic
What C bit-level operator corresponds to $\neg$?
Back: `~`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary c
Tags: binary c17
<!--ID: 1707774068116-->
END%%
@ -102,7 +102,7 @@ Basic
What C bit-level operator corresponds to $\land$?
Back: `&`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary c
Tags: binary c17
<!--ID: 1707774068124-->
END%%
@ -111,7 +111,7 @@ Basic
What C bit-level operator corresponds to $\lor$?
Back: `|`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary c
Tags: binary c17
<!--ID: 1707774068132-->
END%%
@ -120,7 +120,7 @@ Basic
What C bit-level operator corresponds to $\oplus$?
Back: `^`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary c
Tags: binary c17
<!--ID: 1707774068137-->
END%%
@ -129,7 +129,7 @@ Basic
What is the value of `~0b00001111`?
Back: `0b11110000`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary c
Tags: binary c17
<!--ID: 1707774068142-->
END%%
@ -138,7 +138,7 @@ Basic
What is the value of `0b00001111 & 0b11111111`?
Back: `0b00001111`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary c
Tags: binary c17
<!--ID: 1707774068151-->
END%%
@ -147,7 +147,7 @@ Basic
What is the value of `0b00001111 | 0b11111111?
Back: `0b11111111`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary c
Tags: binary c17
<!--ID: 1707774068161-->
END%%
@ -156,7 +156,7 @@ Basic
What is the value of `0b00001111 ^ 0b11111111`?
Back: `0b11110000`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary c
Tags: binary c17
<!--ID: 1707774068167-->
END%%
@ -174,7 +174,7 @@ Basic
What C bit-level operator corresponds to set complement?
Back: `~`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary c set
Tags: binary c17 set
<!--ID: 1707774068179-->
END%%
@ -183,7 +183,7 @@ Basic
What C bit-level operator corresponds to $\cup$?
Back: `|`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary c set
Tags: binary c17 set
<!--ID: 1707774068186-->
END%%
@ -192,7 +192,7 @@ Basic
What C bit-level operator corresponds to $\cap$?
Back: `&`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tags: binary c set
Tags: binary c17 set
<!--ID: 1707774068192-->
END%%

View File

@ -1,313 +0,0 @@
---
title: AWK
TARGET DECK: Obsidian::STEM
FILE TAGS: linux::cli posix::awk
tags:
- awk
---
## Overview
%%ANKI
Basic
How was the name `awk` derived?
Back: By taking the first initials of the original three creators.
<!--ID: 1706726911434-->
END%%
%%ANKI
Basic
What does the term `awk` refer to?
Back: Both the `awk` program and the `awk` language.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706726911439-->
END%%
> Dark corners are basically fractal - no matter how much you illuminate, there's always a smaller but darker one. #quote
The above quote is attributed to Brian Kernighan (one of the authors of the [[C]] K&R book).
## Setup
Robbins suggests executing command `set +H` on [[bash]] startup to disable [[C]] shell-style command history.
## Structure
`awk` applies actions to lines matching specified patterns. In this way `awk` is said to be data-driven - we specify the lines `awk` should act on and `awk` is responsible for finding and acting on them. Instructions are provided via a **program**.
%%ANKI
Basic
What is the basic function of `awk`?
Back: To apply actions to lines matching specified patterns.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706726911445-->
END%%
An `awk` program consists of **rules**, each made up by a **pattern** and **action**. For example:
```awk
BEGIN { print "hello world" }
pattern { action }
```
%%ANKI
Basic
An `awk` program consists of a series of what?
Back: Rules.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706726911448-->
END%%
%%ANKI
Basic
A rule found in an `awk` program consists of what two parts?
Back: A pattern and an action.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706726911451-->
END%%
%%ANKI
Basic
A standalone `awk` program usually has what shebang?
Back: `#!/bin/awk -f`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706726911454-->
END%%
%%ANKI
Basic
Write the `awk` command that searches file `mail-list` for string `li`.
Back:
```bash
$ awk '/li/' mail-list
```
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706819150999-->
END%%
%%ANKI
Basic
How is an `awk` rule without a pattern interpreted?
Back: As applying the specified action for every input line.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706822165633-->
END%%
%%ANKI
Basic
How is an `awk` rule without an action interpreted?
Back: As printing every line matching the specified pattern.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706822165637-->
END%%
%%ANKI
Basic
Describe what the following command does in in a single sentence:
```bash
$ awk 'length($0) > 80' data
```
Back: Prints every line of `data` that is longer than `80` characters.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706822165641-->
END%%
`awk` reads in files in units called **records**. Each record is automatically broken up into chunks called **fields**. By default, a record corresponds to a single line. `$0` would then refer to the entire line and `$1` would refer to the first field of this line.
%%ANKI
Basic
In `awk`, what does a "record" refer to?
Back: The unit that input is read in.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981378-->
END%%
%%ANKI
Basic
In `awk`, what does a "field" refer to?
Back: A specific part of a record.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981395-->
END%%
%%ANKI
Basic
By default, fields are separated by what?
Back: A sequence of spaces, tabs, and newlines.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259928-->
END%%
%%ANKI
Basic
How does `awk` define whitespace?
Back: As *only* spaces, tabs, and newlines.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259932-->
END%%
%%ANKI
Basic
How are fields referenced?
Back: Via the `$$` sign.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259933-->
END%%
%%ANKI
Basic
What is `$0` a placeholder for?
Back: An entire record.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706823790230-->
END%%
%%ANKI
Basic
What is `$1` a placeholder for?
Back: The first field of a record.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706823790233-->
END%%
%%ANKI
Basic
How can you remove trailing fields of `$0`?
Back: Assign a smaller value to `$NF`.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707491299858-->
END%%
%%ANKI
Basic
How do you typically recompute the value of `$0`?
Back: `$1 = $1`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707491299860-->
END%%
%%ANKI
Basic
*Why* does the following not output what you want?
```bash
$ ls -l | awk '{ OFS=":"; print $0 }'
```
Back: `$0` wasn't recomputed so it maintains the previous `OFS` value.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707491299862-->
END%%
%%ANKI
Basic
How can you update the following to behave correctly?
```bash
$ ls -l | awk '{ OFS=":"; print $0 }'
```
Back:
```bash
$ ls -l | awk '{ OFS=":"; $1 = $1; print $0 }'
```
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707491299864-->
END%%
%%ANKI
Basic
When is the behavior of the field reference operator (i.e. `$$`) undefined?
Back: When given a negative number.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259938-->
END%%
%%ANKI
Basic
How is the `BEGIN` pattern interpreted?
Back: Code associated with it executes before any input is read.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706823790236-->
END%%
%%ANKI
Basic
How is the `END` pattern interpreted?
Back: Code associated with it executes after all input has been read.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706823790239-->
END%%
%%ANKI
Basic
Describe what the following command does in in a single sentence:
```bash
$ awk 'NF > 0' data
```
Back: Prints every record in `data` with at least one field.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706823927382-->
END%%
%%ANKI
Basic
Describe what the following command does in in a single sentence:
```bash
$ awk 'END { print NR }' data
```
Back: Prints the number of records in `data`.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706824091124-->
END%%
%%ANKI
Basic
Describe what the following command does in in a single sentence:
```bash
$ awk 'NR % 2 == 0' data
```
Back: Prints every even-numbered record in `data`.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706824091128-->
END%%
`awk` is said to be a "line-oriented" language. Every rule's action must begin on the same line as the pattern.
%%ANKI
Basic
When can a rule's pattern and action exist on different lines?
Back: Only when using backslash continuation.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706883732944-->
END%%
%%ANKI
Basic
What is the output of the following?
```bash
$ echo ' abc' | awk '{ print }'
```
Back: ` abc` (with leading whitespace)
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707618833559-->
END%%
%%ANKI
Basic
What is the output of the following?
```bash
$ echo ' abc' | awk ' { $1 = $1; print }'
```
Back: `abc` (without leading whitespace)
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707618833561-->
END%%
%%ANKI
Basic
How is `$$0` rebuilt after assignment `$1 = $1`?
Back: By intercalating `OFS` between values of `$1` through `NF`.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707618833562-->
END%%
## References
* Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)

View File

@ -1,124 +0,0 @@
---
title: Regular Expressions
TARGET DECK: Obsidian::STEM
FILE TAGS: linux::cli posix::awk regexp
tags:
- awk
---
## Overview
Most `awk` patterns are regular expressions delimited with `/`. We can use `~` and `!~` to perform more complicated regexp filtering:
```bash
# Matches any line with `li` somewhere.
$ awk '/li/' data
$ awk '$0 ~ /li/' data
# Matches any line with `li` somewhere in the first field.
$ awk '$1 ~ /li/' data
```
`awk`'s implementation of regexps are a superset of [[posix/regexp|POSIX EREs]].
%%ANKI
Basic
What is the result of the following?
```bash
$ echo aaaabcd | awk '{ sub(/a+/, "<A>"); print }'
```
Back: `<A>bcd`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707231831794-->
END%%
%%ANKI
Basic
How is the following equivalently written using `~`?
```bash
$ awk '/li/' data
```
Back:
```bash
$ awk '$0 ~ /li/' data
```
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1710940567898-->
END%%
%%ANKI
Basic
What operator is used for regexp matching?
Back: `~`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1710940567902-->
END%%
%%ANKI
Basic
What operator is used for regexp non-matching?
Back: `!~`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1710940567906-->
END%%
%%ANKI
Basic
How do we write a pattern where the second field matches regexp `/li/`?
Back:
```bash
$ awk '$2 ~ /li/' {...}
```
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1710940567909-->
END%%
%%ANKI
Cloze
In `awk`, `/.../` is to a {regexp} constant whereas `"..."` is to a {string} constant.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707232573706-->
END%%
%%ANKI
Basic
How are string constants processed differently from regexp constants?
Back: The string constant is scanned twice.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707232573708-->
END%%
%%ANKI
Basic
What term describes a regexp that isn't a regexp constant?
Back: A dynamic regexp.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707232573710-->
END%%
%%ANKI
Basic
How is `*` escaped in a regexp constant?
Back: `/\*/`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707232573712-->
END%%
%%ANKI
Basic
How is `*` escaped in a string constant (dynamic regexp)?
Back: `"\\*"`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707232573713-->
END%%
%%ANKI
Basic
Why is it recommended to avoid using `^` and `$$` in `RS`?
Back: These anchors match the beginning and end of a string, not of a line.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981375-->
END%%
## References
* Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)

View File

@ -1,386 +0,0 @@
---
title: Variables
TARGET DECK: Obsidian::STEM
FILE TAGS: linux::cli posix::awk
tags:
- awk
---
## Overview
Variables are defined like `var=val`. They can be specified in two different places:
1. Via the `-v` command line flag. Using this allows accessing the variable value from within a `BEGIN` rule.
2. In the file list. Using this allows accessing the variable value in all subsequent file processing.
%%ANKI
Basic
Where in an `awk` invocation can variables be assigned?
Back: As a `-v` argument or in the file list.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706885111450-->
END%%
%%ANKI
Basic
The `-v` flag was introduced to accommodate what functionality?
Back: Accessing variables from a `BEGIN` rule.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706885111454-->
END%%
%%ANKI
Basic
Describe what the following command does in in a single sentence:
```bash
$ awk 'program' pass=1 data pass=2 data
```
Back: Evaluates `'program'` against the `data` file twice with a different value of `pass` on each run.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706885111457-->
END%%
%%ANKI
Basic
How is `stdin` specified in `awk`'s file list?
Back: `-`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1706973587236-->
END%%
## Predefined Variables
There exists a number of useful predefined variables:
* `NR` (**N**umber of **R**ecords)
* The 1-indexed number of records so far read.
* The count includes the current record.
* `FNR` (**F**ile **N**umber of **R**ecords)
* The 1-indexed number of records so far read from the current file.
* The count includes the current record.
%%ANKI
Cloze
The {`NR`} variable specifies the {number of read input records}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259922-->
END%%
%%ANKI
Cloze
The {`FNR`} variable specifies the {number of read input records for the current file}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259925-->
END%%
* `RT` (**R**ecord **T**ext)
* The matching separator used to distinguish the currently read record.
%%ANKI
Cloze
The {`RT`} variable matches the {input characters that matched `RS`}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
Tags: gnu::awk
<!--ID: 1710938533567-->
END%%
%%ANKI
Basic
Barring the final record, when is `RT` always equal to `RS`?
Back: When `RS` is a string containing a single character.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
Tags: gnu::awk
<!--ID: 1710938533571-->
END%%
%%ANKI
Basic
Why is the gawk `RT` variable unnecessary in POSIX `awk`?
Back: Because POSIX `awk` does not permit setting `RS` to a regexp.
Tags: gnu::awk
<!--ID: 1710939301099-->
END%%
* `RS` (**R**ecord **S**eparator)
* The separator used to distinguish records from one another.
* Defaults to `"\n"`.
| `RS == ??` | Description |
| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `"\n"` | Records are separated by the newline character. This is the default. |
| *any single character* | Records are separated by each occurrence of the character. Multiple successive occurrences delimit empty records. |
| `""` | Records are separated by one or more blank lines. Leading/trailing newlines in a file are ignored. If `FS` is a single character, then `"\n"` also serves as a field separator. |
| *regexp* | Records are separated by occurrences of characters that match *regexp*. Leading/trailing matches delimit empty records. (**GNU only**) |
%%ANKI
Cloze
The {`RS`} variable is used to change the {record separator}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981382-->
END%%
%%ANKI
Basic
What is the default value of `RS`?
Back: `"\n"`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981380-->
END%%
%%ANKI
Basic
How is `RS = "ab"` interpreted in POSIX `awk`?
Back: As if we had instead written `RS = "a"`.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1710939301103-->
END%%
%%ANKI
Basic
How is `RS = "ab"` interpreted in GNU `awk`?
Back: As a regex matching strings `"ab"`.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
Tags: gnu::awk
<!--ID: 1710939301107-->
END%%
%%ANKI
Cloze
If `RS` is a string with {more than one character}, it is treated as a {regexp}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
Tags: gnu::awk
<!--ID: 1707310981384-->
END%%
%%ANKI
Basic
What value of `RS` may `gawk` not process correctly?
Back: A regexp with optional trailing part, e.g. `AB(XYZ)?`.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
Tags: gnu::awk
<!--ID: 1707310981390-->
END%%
%%ANKI
Basic
What implementation detail inspires avoiding `RS = "\0"`?
Back: Most `awk` implementations store strings internally as C-style strings.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981392-->
END%%
%%ANKI
Basic
What equivalent assignment do most `awk` implementations interpret `RS = "\0"` as?
Back: `RS = ""`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981394-->
END%%
%%ANKI
Basic
How is `RS = ""` interpreted?
Back: `""` indicates one or more blank lines should be treated as the record separator.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707829409163-->
END%%
%%ANKI
Basic
What distinguishes `RS` value `""` and `\n\n+`?
Back: When set to the former, `awk` strips leading/trailing newlines from the file.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
Tags: gnu::awk
<!--ID: 1707829409170-->
END%%
%%ANKI
Basic
What distinguishes `RS` value `""` and `\n`?
Back: The former separates on one or more *blank* lines, not just a newline character.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1708867924207-->
END%%
%%ANKI
Basic
What regexp is closest to mirroring `RS = ""` behavior?
Back: `\n\n+`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1708867924213-->
END%%
%%ANKI
Cloze
If `RS = ""` and `FS` is set to {a single character}, the {newline character} *always* acts as a field separator.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707829509362-->
END%%
* `NF` (**N**umber of **F**ields)
* The 1-indexed number of fields found in the current record.
%%ANKI
Basic
What is the arithmetical value of `${NF + 1}`?
Back: `0`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259936-->
END%%
%%ANKI
Basic
What is the printed value of `${NF + 1}`?
Back: `""`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259935-->
END%%
%%ANKI
Basic
What value is `$${NF + 1}` given when we run `${NF + 2} = "test"`?
Back: `""`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707491299854-->
END%%
%%ANKI
Cloze
The {`NF`} variable specifies the {number of fields in the current record}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259930-->
END%%
%%ANKI
Basic
What two things does incrementing `NF` do?
Back: Creates the field and rebuilds the record.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707829863717-->
END%%
%%ANKI
Basic
What two things does decrementing `NF` do?
Back: Throws away fields and rebuilds the record.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707829863726-->
END%%
* `FS` (**F**ield **S**eparator)
* The separator used to distinguish fields from one another.
| `FS == ??` | Description |
| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `" "` | Fields are separated by runs of whitespace. Leading/trailing whitespace is ignored. This is the default. |
| *any other single character* | Fields are separated by each occurrence of the character. Multiple successive occurrences delimit empty fields, as do leading/trailing occurrences. |
| `"\n"` | Specific instance of the above row. It is used to treat the record as a single field (assuming newlines separate records). |
| *regexp* | Fields are separated by occurrences of characters that match *regexp*. Leading/trailing matches delimit empty fields. |
| `""` | Each individual character in the record becomes a separate field. (**GNU only**) |
%%ANKI
Cloze
The {`FS`} variable is used to change the {field separator}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707618833549-->
END%%
%%ANKI
Cloze
{`FS`} is to `awk` as {`IFS`} is to Bash.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
END%%
%%ANKI
Basic
What is the default value of `FS`?
Back: `" "`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707618833552-->
END%%
%%ANKI
Basic
What value of `FS` is specially handled?
Back: `" "`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707618833555-->
END%%
%%ANKI
Basic
How is `FS = " "` interpreted?
Back: As a contiguous sequence of spaces, tabs, and newlines.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707618833556-->
END%%
%%ANKI
Basic
What distinguishes `FS` value `" "` and `[ \t\n]+`?
Back: When set to the former, `awk` strips leading/trailing whitespace from each record.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707618833558-->
END%%
%%ANKI
Cloze
Setting `FS` to {`""`} allows examining {each character of a record separately}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
Tags: gnu::awk
<!--ID: 1707756447064-->
END%%
%%ANKI
Basic
How is `FS = ""` interpreted in POSIX `awk`?
Back: As a no-op.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1710939301112-->
END%%
%%ANKI
Basic
How is `FS = ""` interpreted in GNU `awk`?
Back: Each individual character in the record becomes a separate field.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
Tags: gnu::awk
<!--ID: 1710939301117-->
END%%
%%ANKI
Cloze
If `RS` has its default value, setting `FS` to {`"\n"`} treats the {record as the single field}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707756447067-->
END%%
%%ANKI
Basic
What value of `FS` ensures `$1 = $0`?
Back: `RS`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707756447069-->
END%%
%%ANKI
Basic
*Why* does `awk` support a CSV mode?
Back: Because CSV fields may contain commas and newlines.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707756447071-->
END%%
* `OFS` (**O**utput **F**ield **S**eparator)
* Specifies the field separator used on printing.
%%ANKI
Cloze
The {`OFS`} variable is used to change the {output field separator}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707829783790-->
END%%
## References
* Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)