CMPT 214 - AWK in Programming Principles
21 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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?

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?

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?

<p>This command searches for lines containing the letter 'h' in the file named 'cars'.</p> Signup and view all the answers

What allows AWK to make changes to lines in a file unlike grep?

<p>AWK can make changes to lines due to its ability to manipulate data and execute scripts, while grep is used mainly for searching patterns.</p> Signup and view all the answers

What role do patterns play in AWK?

<p>Patterns in AWK are regular expressions used to identify lines within files.</p> Signup and view all the answers

Explain the importance of BEGIN and END patterns in AWK.

<p>BEGIN and END patterns allow actions to be executed before processing any input lines and after all lines have been processed, respectively.</p> Signup and view all the answers

What is the default action taken by AWK when a pattern matches a line?

<p>The default action is to print the line that matches the pattern.</p> Signup and view all the answers

Describe how you would utilize a pipe in AWK.

<p>A pipe is used to pass the output of an AWK command to another command for further processing.</p> Signup and view all the answers

What are associative arrays in AWK?

<p>Associative arrays in AWK are hash maps that allow the storage and retrieval of data using string keys.</p> Signup and view all the answers

How can built-in functions in AWK assist in text manipulation?

<p>Built-in functions provide various methods to manipulate strings, numbers, and arrays for processing text data.</p> Signup and view all the answers

What is the primary function of SED as a stream editor?

<p>SED acts as a batch editor that can edit multiple files simultaneously based on pattern matching and substitution.</p> Signup and view all the answers

How does AWK process the contents of the file specified with the -f option?

<p>AWK processes the file specified with the -f option by executing the commands defined in the script file on the input data.</p> Signup and view all the answers

What is initialized when a union variable is set without a member name?

<p>The first member of the union is initialized.</p> Signup and view all the answers

How is dynamic typing achieved using unions in a structure?

<p>By combining an enum with a union that can hold different data types, allowing the storage of any of those types within a single variable.</p> Signup and view all the answers

What is the purpose of argc in command line argument handling?

<p>It holds the number of command line arguments passed to the program.</p> Signup and view all the answers

What does argv represent in the context of command line arguments?

<p>It is an array of character pointers pointing to the strings of command line arguments.</p> Signup and view all the answers

How can a member of a union be explicitly initialized?

<p>By using the member name in the initialization syntax, like <code>union mixed x = {.f = 123.456};</code>.</p> Signup and view all the answers

What needs to be updated whenever a union member is written to?

<p>The corresponding enum symbolType has to be updated.</p> Signup and view all the answers

What happens if you initialize a union with a value without specifying a member?

<p>The first member of the union is set to the specified value.</p> Signup and view all the answers

What is a potential downside of using unions for dynamic typing?

<p>It requires constant checking and updating of the type variable to ensure correct data access.</p> 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 in awk.

AWK - Patterns and Actions

  • Patterns are regular expressions.
  • BEGIN and END are special patterns for pre/post-processing.
  • BEGIN and END 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] or sed [-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, so SQUARE(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, like x20, 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 (an argv 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.

Quiz Team

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.

Use Quizgecko on...
Browser
Browser