AWK 1

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

When awk takes a line from input, what variable does it assign it to?

$0 It then breaks the line into fields by white space or tab, and saves them in $1, $2, up to the total number of fields

What are the two variables in a record? What are they used for?

$0: is a variable, saved the entire record(whole line) NR: number of records (number of lines)

what logical operations can be used on fields?

&&, ||, !

What are the arithmetic operations that can be used on fields?

+, -, *, /, %, ^

Given the following line from data.txt and the following command, what will be printed? Amy Lin | 824-5164 | UAH ITSC | AL 35899 awk -F "|" '{print NF, ":",$1, $2}' data.txt

4: Amy Lin 824-5156 makes "|" the input field separator

Given the following line from data.txt and the following command, what will be printed? Amy Lin | 824-5164 | UAH ITSC | AL 35899 awk -F "[ |]" '{print NF, ":",$1, $2, NF}' data.txt

7 :Amy Lin Both "|" and " " are the input field separators

What is AWK?

AWK is a UNIX programmin language used for manipulating data and generating reports

Describe what AWK does.

AWK scans input (file/stdin) line by line (like SED). Searches for lines matching a specified pattern.

Describe the BEGIN block.

BEGIN is followed by an action block that is executed before awk processes lines from the input file. Can be used to initialize variables, change the values of some default variables, such as FS OFS, etc awk 'BEGIN{FS=":"; OFS="\t"; ORS="\n\n" {print $1 $2, $3}' file

What are the AWK built-in variables?

FS: field separator NF: number of fields (each line) separated by FS $n: the nth field/column of a record (also called Positional Parameters) OFS: Output field separator NR: internal variable: number of records

What is a field?

Fields are segments of a record separated by a field separator, by default, it is either a whitespace or tab

Describe the END block.

Followed by actions handled after all input lines have been processed. can be used to do some statistics analysis on the input datafile(sum, average). awk '/Mary/{count++}END{print "Mary was found", count,\"times."}' input_file

Where can input for awk be received from?

From file or STDIN, or pipe ( output stream of previous command )

How many actions per line are allowed in script?

One. No need for semicolon at the end. No single quotes for the whole awk command when written in script. /Pattern/{ action statement one action statement two }

How do you specify the actions for AWK to to perform?

Specify actions by enclosing instructions in curly brackets {...}

How does awk perform the actions on the fields?

Takes next line from input file and puts $0 and performs the actions on it,.. until all the lines have been processed

How can you define your own FS: (field separator) with awk?

Use the -F option

Can you have multiple actions within the curly brackets? If so, how do you separate them?

Yes. Actions are separated by semicolons on one line. '/Pattern/{ action statement one; action statement2}'

Can you use awk commands in script files? If so, give an example.

Yes. awk -f script.awk InputFile

how would you print all lines containing "Mary" using awk?

awk '/Mary/' employee.txt

how would you print 1st and 2nd fields separated by white space for lines starting with Sally using awk?

awk '/^Sally/ {print $1, $2}' employee.txt

What is the AWK command format?

awk 'pattern {action}' InputFile Use single quotes, double quotes will be used inside the awk command.

who would you print the first and fourth fields in two separated lines using awk?

awk '{print $1; print $4}' employee.txt

Since NR is an internal varible, how would you use it in a command?

awk '{print NR, $1, $2}' employee.txt

give an example of how to use printf in awk.

awk '{printf "The name is %-15s, ID is %d\n", $1, $3}' employee.txt %-15s -> left justified 15-space string %8d -> right justified 8-space integer

What is a record?

each line terminated with a newline is a record

How do you specify a pattern for awk?

including Regex, enclosed with forward slashes /.../ awk '/Tom/' employe.txt

What is NF in a field?

number of fields in each record, so it can vary from line to line

what operators can be used to compare numbers and strings?

number: <, <=, ==, !=, >=, > string matching regex: ~, !~ ex: awk '$3 >= 124 {print NR,$0}' employee.txt

Explain what the following command will do: awk 'pattern' InputFile

print (default action) lines that matches the pattern

Explain what the following command will do: who | awk '{print $1}'

print out 1st field (user name) of the output from who

If you want to print the OFS (comma) in the following instruction, what do you need to do? awk '{print $1, $2}' employee.txt

put the comma in double quotes. " , " By default, commas are converted to whitespace

Explain what the following command will do: awk '{action}' InputFile

the action will be performed on all lines

Explain what the following command will do: awk 'pattern {action}' InputFile

the action will be performed on lines which match the pattern

what are "~" and "!~" used for?

they are used to match an expression within a field. ex: awk '$2 ~ /Jones/' employee.txt

A new line "\n" is added by default for every print, how do you stop this if you don't want a new line?

use printf


संबंधित स्टडी सेट्स

Angular Interview study questions and answers

View Set

Quiz 6 - Key concepts and predicted questions

View Set

Fundamentals for Nursing Exam 2: Lippincott Quiz Practice and Book Practice Questions

View Set

Labor and Delivery Processes - ATI CH 11

View Set

None for the Road exam questions (not the practice exam)

View Set