Shared Flashcard Set

Details

CST334 - awk
awk programming language
15
Computer Science
Undergraduate 3
03/01/2021

Additional Computer Science Flashcards

 


 

Cards

Term

A .csv file usually has the same number of fields on every line.  Output the unique values of the number of fields in file brain.csv, assuming fields are separated by whitespace.

Definition

awk '{print NF}' brain.csv | sort | unique

Term

Output the values in the second field of file dat.txt

Definition

awk '{print $2}' dat.txt

Term

Fields 1 and 2 of file data.csv have a name and a count, respectively. The same name can appear on multiple lines of the file. Output the sum of the counts for each name, with name in output field 1 and total count in field 2.

Definition

awk '{sum[$1] += $2} END{ for (s in sum) print s" "sum[nm]}' data.csv

Term

Output fields 1 and 4 of the file baz.txt, separated by a comma.

Definition

awk '{print $1","$4}' baz.txt

 

alternatively, specify the output field separator:

 

awk 'BEGIN{OFS=","}{print $1,$4}' baz.txt

Term

Write an awk script that outputs fields 2 and 3 of every line of that contains the text "Run JOB".

Definition

/Run JOB/ {

  print $2, $3

}

Term

Write a standalone awk script that prints the sum of the (numeric) values in the 5th input field.

Definition

{ total += $5 }

END { print total }

Term

At the command line, output lines of bar.txt that are no more than 72 characters long.

Definition

awk '{if (length($0) <= 72) print}' bar.txt

Term

At the command line, print the 2rd field of every line of msh.c that begins with '#include '.

Definition

awk '/^#include / { print $2 }' msh.c

Term

Write an awk script to output every line that follows a line whose first field is 'problem'.

Definition

# this example shows how you can "remember"

# something using a variable

BEGIN {

  print_next = 0

}

{

  if (print_next == 1) {

    print        # by default print outputs $0

    print_next = 0

  }

}

$1 ~ /problem/ {

   # remember to print next line

   print_next = 1

}

 

Term

Run the awk script preprocess.awk on file housing.csv.

Definition

awk -f preprocess.awk housing.csv

Term

Write a standalone awk script that will output the percent of lines in which the value of the first field is less than 100. 

 

Ignore empty lines (in other words, don't use them in your percentage calculation).

Definition

{

  if (NF > 0 && $1 < 100) m++

  if (NF > 0) n++

}

END {

  if (n > 0) 

    print 100*(m/n)" %"

  else 

    print "empty file"

}

Term

Output the number of lines in temp.txt that have exactly two fields.

Definition

awk '{if (NF == 2) n++} END{print n}' temp.txt

Term

Output all the unique values that appear in the second field of file foo.txt

 

You can use pipe and additional commands in addition to awk.

Definition

awk '{print $2}' foo.txt | sort | uniq

Term

Using only awk, output the last line of file baz.csv

Definition

awk '{x=$0} END{print x}'

Term

Pipe the output of 'ls -l' to the awk script you've saved as 'summary.awk'

Definition

ls -l | awk -f summary.awk

Supporting users have an ad free experience!