awk Flashcards

1
Q

awk definition

A

pattern matching, record manipulation, programming.

awk processes files where the content is regarded as a set of records containing a consistent set of fields (like a table).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

awk syntax

A

awk options ‘selection_criteria {action}’ file(s)

ex.

awk /Dairy/ {print $1,$3} gdbase

command Options SelectionCriteria Action Filename(s)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

“For each matching ___, perform the given ___.”

A

Line
Action

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If no action is specified, the default action is ___. In this case awk acts like ___.

A

Print
grep

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The action print without field specifiers (such as $1 and $3) outputs

A

each line matched

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If no selection criteria are specified, all lines are selected for processing. In this case awk acts like?

A

cat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

awk’s key distinction is that specific pieces of a record (or line) can be ___ & ___ ?

A

selected and processed.

grep just selects the line, and does no processing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

awk is only possible if the input is organized in

A

some regular manner

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

awk ‘/Fish/ {print $1}’ gdbase

A

awk ‘/Fish/ {print $1}’ gdbase

  • This prints the first field of lines that contain the string “Fish”.
  • $ is the field operator – it indicates the field of the record to consider. For example, $1 is
    interpreted as the value of the record’s first field.

Note: $0 is a convenience variable that represents the entire line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

awk ‘/Fish/ {print $1, $3, $2}’ gdbase

A

awk ‘/Fish/ {print $1, $3, $2}’ gdbase

  • Prints fields 1, 3, 2 for each record, with a space between each. The comma indicates that a space will separate the fields.
  • Note: awk views a string as a variable unless it is inside double quotes or inside the slashes / /.
  • Strings inside / / are regular expression strings for pattern matching.
  • For example, numbers are regarded as a string in this context:
    awk ‘{print 1234 $1} gdb
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Selecting Lines by Field Value

awk ‘$5 == ”y” {print $1, $3, $2}’ gdbase

A

Selecting Lines by Field Value
awk ‘$5 == ”y” {print $1, $3, $2}’ gdbase

  • Lines where field 5 contains only the character y, are selected for processing.
  • Processing here is print field 1 followed by field 3 then field 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Use Other Relational Operators for Selecting Lines by Field Value
awk ‘$5 == ”y” && $3<4.00 {print $1, $3, $2}’ gdbase

A

Use Other Relational Operators for Selecting Lines by Field Value
awk ‘$5 == ”y” && $3<4.00 {print $1, $3, $2}’ gdbase

  • Use && as logical AND and || as logical OR.
  • Enclose expressions in brackets () to group them like in Java.
  • The NOT operator is !
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

You can use these operators between target strings:

awk ‘/Dairy/||/Meat/ && $3<4.00 {print $1, $3, $2}’ gdbase

A

You can use these operators between target strings:
awk ‘/Dairy/||/Meat/ && $3<4.00 {print $1, $3, $2}’ gdbase

  • This will print fields 1, 3 and 2 for lines that contain “Dairy” or lines that contain “Meat” where the 3rd field is less than 4.00.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

accumulator variable

A

printing sum

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can Search a Field for a Target

awk ‘$3~/.89$/’ gdbase

A

Can Search a Field for a Target
awk ‘$3~/.89$/’ gdbase

  • Use the ~ operator to search the 3rd field for the string .89 at the end of the field, and select those records for processing.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Defined variables
 NF =>
 NR =>
 FS =>
 OFS =>
 ORS =>

A

 NF => number of fields in a record
 NR => number of records (lines) in the file
 FS => Field Separator
 OFS => Output Field Separator
 ORS => Output Record Separator

17
Q

NF (Defined variables)

A

number of fields in a record

18
Q

NR

A

NR => number of records (lines) in the file