GIT – Basic examples of search lines.

  • What is the difference between awk and ?

sed is a stream editor. It works with streams of characters on a per-line basis. It has a primitive programming language that includes goto-style loops and simple conditionals (in addition to pattern matching and address matching). There are essentially only two “variables”: pattern space and hold space. Readability of can be difficult. Mathematical operations are extraordinarily awkward at best.

awk is oriented toward delimited fields on a per-line basis. It has much more robust programming constructs including if/elsewhiledo/while and for (C-style and array iteration). There is complete support for variables and single-dimension associative arrays plus (IMO) kludgey multi-dimension arrays. Mathematical operations resemble those in C. It has printf and functions. The “K” in “AWK” stands for “Kernighan” as in “Kernighan and Ritchie” of the book “C Programming Language” fame (not to forget Aho and Weinberger). One could conceivably write a detector of academic plagiarism using awk.

Basic search examples of strings and basic examples of boolean searchs

Basic string searches.

  • Search for  string  ”word” in file called datafile.

awk  ’/word/’  datafile

  • Search for string “word” and display  fields 1 and 4  from file called datafile.

awk ‘/word/  {$1 $4}’ datafile

Basic conditional  searches

(The double quotes between 5 and 6 will create a space between the numbers displayed for a more proper format).

  • Search for string that its fifth field has a numeric value larger than 4 and print the fifth and sixth field from file called datafile. 

awk  ’ $5>4  {print $5″  ” $6}’ datafile

  • Search for string that its fifth field has a numeric value smaller  than 4 and print the fifth and sixth field from file called datafile. 

awk  ’$5<4 {print $5 ”  ”$6}’ datafile

  • Search for string that its fifth field has a numeric value that is not 4 and print the fifth and sixth field from file called datafile. 

awk  ’$5 != 4 {print $5 ” ” $6}’ datafile

Basic string and numerical conditional  searches (Boolean expressions).

  •  Search for string that its second field has a value of NE and its fifth field is not three and print the first and sixth field.

awk  ’$2=/NE/ && $5!=3  {print $1 ” ” $6}’ datafile

  •  Search for string that its second field has a value of NE or its fifth field is equal to three and print the first and sixth field.

awk  ’$2=/NE/ || $5=3 {print $5″ ” $6}’ datafile

The printf commandawk

Print Friendly

Comments

comments

Bài viết liên quan