Podcast
Questions and Answers
What is AWK primarily used for in programming?
What is AWK primarily used for in programming?
AWK is primarily used for text parsing and data manipulation via pattern matching.
What are some programming constructs that AWK shares with C?
What are some programming constructs that AWK shares with C?
AWK shares constructs such as conditionals, loops, numeric and string variables, and regular expressions with C.
How would you execute an AWK script using a file with the appropriate command?
How would you execute an AWK script using a file with the appropriate command?
You would use the command gawk -f script.awk filename
to execute an AWK script with a specified file.
In the example command awk '/h/' cars
, what does the program do?
In the example command awk '/h/' cars
, what does the program do?
Signup and view all the answers
What allows AWK to make changes to lines in a file unlike grep?
What allows AWK to make changes to lines in a file unlike grep?
Signup and view all the answers
What role do patterns play in AWK?
What role do patterns play in AWK?
Signup and view all the answers
Explain the importance of BEGIN and END patterns in AWK.
Explain the importance of BEGIN and END patterns in AWK.
Signup and view all the answers
What is the default action taken by AWK when a pattern matches a line?
What is the default action taken by AWK when a pattern matches a line?
Signup and view all the answers
Describe how you would utilize a pipe in AWK.
Describe how you would utilize a pipe in AWK.
Signup and view all the answers
What are associative arrays in AWK?
What are associative arrays in AWK?
Signup and view all the answers
How can built-in functions in AWK assist in text manipulation?
How can built-in functions in AWK assist in text manipulation?
Signup and view all the answers
What is the primary function of SED as a stream editor?
What is the primary function of SED as a stream editor?
Signup and view all the answers
How does AWK process the contents of the file specified with the -f option?
How does AWK process the contents of the file specified with the -f option?
Signup and view all the answers
What is initialized when a union variable is set without a member name?
What is initialized when a union variable is set without a member name?
Signup and view all the answers
How is dynamic typing achieved using unions in a structure?
How is dynamic typing achieved using unions in a structure?
Signup and view all the answers
What is the purpose of argc in command line argument handling?
What is the purpose of argc in command line argument handling?
Signup and view all the answers
What does argv represent in the context of command line arguments?
What does argv represent in the context of command line arguments?
Signup and view all the answers
How can a member of a union be explicitly initialized?
How can a member of a union be explicitly initialized?
Signup and view all the answers
What needs to be updated whenever a union member is written to?
What needs to be updated whenever a union member is written to?
Signup and view all the answers
What happens if you initialize a union with a value without specifying a member?
What happens if you initialize a union with a value without specifying a member?
Signup and view all the answers
What is a potential downside of using unions for dynamic typing?
What is a potential downside of using unions for dynamic typing?
Signup and view all the answers
Study Notes
CMPT 214 - Programming Principles and Practice
- Lecture 19 covers miscellaneous and advanced Bash tools, C, and C preprocessor.
- Required reading materials include Sobell: Chapters 14 and 15, Kochan: Chapter 12, and Kochan: Chapter 16.
AWK - Sobell 14
- AWK is an acronym for the authors' names (Aho, Weiderhold, and Kernighan).
- Kernighan wrote the C book.
- AWK is a text parsing tool, data-driven, originally written in 1977.
- It is still being developed and maintained.
- AWK is more data-driven than procedural.
- AWK is available in links from the text.
Principles of AWK
- AWK uses many C constructs (conditionals, loops).
- It uses numeric and string variables.
- It includes regular expressions and relational expressions.
- It has a
printf
function. - AWK can be used in coprocess execution (gawk - GNU awk).
- The form uses
gawk [options] [program] [file-list]
. - Information on how to use
awk
is provided.
Using AWK
- Simple programs can be run after AWK by using programs within quotes.
- Options such as looking at the man page are available.
- Data manipulation via pattern matching is a core concept of AWK.
- AWK is similar to
grep
, but it changes lines. -
pattern [action]
is a key pattern inawk
.
AWK - Patterns and Actions
- Patterns are regular expressions.
-
BEGIN
andEND
are special patterns for pre/post-processing. -
BEGIN
andEND
specify a range of lines in a file. - The default action is printing the line matched.
- Output can be piped to other commands with standard redirection.
- The
|
symbol uses pipes;|&
is a coprocess command (for 2-way data exchange).
AWK - Variables/Functions/Operators
- AWK uses variables (0,0, 0,1-$n), and built-in functions like FILENAME, FS, NF, NR, OFS, ORS, and RS.
- Operators in AWK are similar to C.
- Associative arrays (hash maps) are used in AWK.
AWK Examples
- AWK examples (using
-f manip.awk *.txt
) are shown for file processing. - The BEGIN block demonstrates initial changes.
- The conditional code searches for
z
values and converts them to uppercase. - The END block signifies the end of processing.
SED - Stream Editor
- SED is a batch editor for multiple files.
- SED uses pattern matching and substitution.
- The form uses
sed [-n] program [file-list]
orsed [-n] -f program-file[file-list]
. - Basic SED functions (editor basics) are shown in the text.
SED - Examples Using What We Already Know
- SED examples using familiar commands (e.g.,
/line/ p' lines
). - SED example using the subs_demo script for specific line substitutions (/line/sentence/p).
Program Portability
- The
#define
statement can be used for program portability. - Example shows using
INT_LEFTMOST_ZERO
to handle logical right shift vs. arithmetic right shift issues across different systems. - This needs to account for the integer size and the
#define INT_LEFTMOST_ZERO
statements
Macros
- Macros can use the preprocessor statement
#define
for code readability. - A form for the
IS_LEAP_YEAR
macro is shown for leap year checking. - Macros can accept arguments, like in
IS_LEAP_YEAR(y)
. - This makes macros more flexible and reusable.
- Macros with the
#
operator create constant strings from macro arguments, important for making macros safe to use, important to wrap macros in brackets to prevent unexpected behavior from variables - In example of the
SQUARE(x)
macro, the#define SQUARE(x) ((x) * (x))
is the proper method of writing the macro so that expression values stay within the macro and don't cause problems in evaluation, soSQUARE(x + 1)
is expressed as((x + 1) * (x + 1))
to provide a safe expression
Macros (Variable Number of Arguments)
- Macros can handle variable numbers of arguments using
...
at the end. - The
_VA_ARGS_
placeholder is used to reference these arguments within the macro. - Example of
DEBUG_PRINTF
with multiple arguments shows how_VA_ARGS_
works.
The # Operator
- The
#
operator within the macro creates constant strings for output. - Example converting
STR(programming in C)
to"programming in C"
- The
#
operator supports double quotes and backslashes. - Provides a common example of
PRINT_INT
using this operator.
The ## Operator
- The
##
operator within the macro joins tokens, allowing for dynamic variable names, likex20
, to be created to be passed to functions - The example shows this through the use of
PRINT_X
Conditional Compilation
-
#ifdef
,#ifndef
,#elif
,#else
,#endif
help select code based on symbolic values and preprocessor definitions. - These can be used to adapt code to different systems (for example, Linux or Windows).
The Conditional Operator
- The conditional operator (
? :
) is a ternary operator (takes three arguments)
Type Qualifiers (register, volatile, restrict)
-
register
hints to the compiler to optimize by storing the variable in a register. -
volatile
indicates that a variable's value can change unexpectedly; for situations like I/O operations, to prevent the compiler from optimizing away assignments or re-evaluation. -
restrict
optimization hint to help avoid multiple references to the same memory address.
Unions
- Unions allow storing different data types in the same memory location.
- Members share memory, only one value can be accessed at a time.
- The compiler manages storage allocation.
Command Line Arguments
-
main
function arguments (argc
,argv
) can handle command line inputs. -
argc
is the count of arguments;argv
is an array (anargv
array of strings) -
argv[0]
is the program name;argv[1...n]
are the arguments.
The goto Statement
- The
goto
statement jumps to a labeled location unconditionally, rather than following the program's sequential flow. Use with caution and usually avoid. This can affect readability and maintainability so it is generally not preferable to other structures.
The Null Statement
- A semicolon by itself is a null statement. Its primary use is as a placeholder for simple commands where no code is necessary, primarily in while, for and do loops
The Comma Operator
- The comma operator separates multiple expressions, evaluating from left to right.
- The value from the comma operator is the rightmost expression in the function.
Next Topics
- Assorted programming principles
- Review
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz focuses on AWK as a text parsing tool within the context of CMPT 214. It covers its usage, syntax, and relation to C constructs, as well as its advanced features. Required readings from Sobell and Kochan provide foundational knowledge for understanding AWK's principles and applications.