List

The name awk comes from the initials of its designers: Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan. The original version of awk was written in 1977 at AT&T Bell Laboratories. In 1985 a new version made the programming language more powerful, introducing user-defined functions, multiple input streams, and computed regular expressions. This new version became generally available with Unix System V Release 3.1.

.awk is a programming language designed to search for match patterns and perform actions on files.

AWK Commands

Download file: wget nishantmunjal.com/dataset/mywords

$awk 'length($1) > 5 {print}' mywords
$awk 'length($1) > 5' mywords
$awk '$1 ~ /^[b,c]/ {print $1}' mywords
The above script print all the words that begin with b or c character. The regular expression is placed between two slash characters.
$awk 'NR % 2 == 0 {print}' mywords 
NR is a built-in variable that refers to the current line being processed. The above program prints each second record of the mywords file. Modulo dividing the NR variable we get an even line. $awk '{print NR, $0}' mywords Here NR variable will print the line number and the $0 variable refers to the whole record.

Download File: wget nishantmunjal.com/dataset/code.c

$awk '{print substr($0, 4)}' code.c
substr() function. It prints a substring from the given string. We apply the function on each line, skipping the first three characters. In other words, we print each record from the fourth character till its end.

The Match Function
The match() is a built-in string manipulation function. It tests if the given string contains a regular expression pattern. The first parameter is the string, the second is the regex pattern.

Download File: wget nishantmunjal.com/dataset/mywords.c

$awk 'match($0, /^[c,b]/)' mywords
The program prints those lines that begin with c or b. The regular expression is placed between two slash characters.
$awk 'match($0, /i/) {print $0 " has i character at " RSTART}' mywords
The match() function sets the RSTART variable; it is the index of the start of the matching pattern. This prints those words that contain the 'i' character. In addition, it prints the first occurrence of the character.
$ awk -F: '{print $1, $7}' /etc/passwd | head -7
$ echo "Jane 17#Tom 23#Mark 34" | awk 'BEGIN {RS="#"} {print $1, "is", $2, "years old"}' 
Jane is 17 years old
Tom is 23 years old
Mark is 34 years old
The RS is the input record separator, by default a newline. In the example, we have relevant data separated by the # character. The RS is used to strip them. AWK can receive input from other commands like echo.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

  Posts

1 2 3
November 2nd, 2020

Churn Emails – Count Number Domain

Write a function count_message_from_domain which reads the file /cxldata/datasets/project/mbox-short.txt. This function builds a histogram using a dictionary to count how many messages have […]

November 2nd, 2020

Churn Emails – Count Number of Messages

Python Project – Churn Emails – Count Number of Messages From Each Email Address Write a function count_message_from_email which reads the file /datasets/project/mbox-short.txt.  […]

November 2nd, 2020

Churn Email: Day of the Week

Python Project – Churn Emails – Find Which Day of the Week the Email was sent Write a function find_email_sent_days which reads […]

October 20th, 2020

Compute the Compound Interest.py

Write a function with name compound_interest that takes three arguments: principle, rate and years in order. the rate is float […]

October 18th, 2020

String Data Type

A string is a sequence of characters. String Data Type str1=”hello” print(type(str1)) Ans: <class ‘str’> str2=’123′ print(type(str2)) Ans: <class ‘str’> […]

October 18th, 2020

String Library

String Replace str=”Hello Bob” print(str) rstr=str.replace(‘Bob’, ‘James’) print(rstr)   This will replace the Bob with James and store it in […]

October 17th, 2020

‘in’ statement in String

  fruits=’banana’ bana in fruits Ans: True Python Function to confirm he vowel in the given input. def is_vowel(l): return […]

September 24th, 2020

awk Marking keywords

Marking keywords In the following example, we mark Java keywords in a source file. $wget nishantmunjal.com/dataset/mark_keywords.awk # the program adds […]

September 24th, 2020

awk Rock-paper-scissors

Rock-paper-scissors Rock-paper-scissors is a popular hand game in which each player simultaneously forms one of three shapes with an outstretched […]

September 24th, 2020

awk Spell Checking

Spell checking We create an AWK program for spell checking. $wget nishantmunjal.com/dataset/spellcheck.awk BEGIN { count = 0 i = 0 […]