0% found this document useful (0 votes)
13 views11 pages

AWK Functions

Uploaded by

ksnyogatuni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

AWK Functions

Uploaded by

ksnyogatuni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

String Functions:
 length(string): Returns the length of the string.

 substr(string, start, length): Returns a substring

of the given string.


 index(string, substring): Returns the position of the

first occurrence of the substring in the string.


 split(string, array, separator): Splits a string

into an array using a specified separator.


 gsub(regular_expression, replacement,

target): Globally substitutes all occurrences of the regular


expression with the replacement in the target.
2. Numeric Functions:
 int(x): Returns the integer part of x.

 sqrt(x): Returns the square root of x.

 rand(): Returns a random floating-point number between 0

and 1.
 sin(x), cos(x), atan2(y, x): Trigonometric functions.

3. Array Functions:
 delete array[index]: Deletes an element from an array.

 length(array): Returns the number of elements in an

array.
 for (var in array): Iterates over the elements of an

array.
4. Control Flow Functions:
 if (condition) { statements } else {

statements }: Conditional statement.


 while (condition) { statements }: While loop.
 for (initialization; condition; increment) {

statements }: For loop.


5. File and Input Functions:
 getline: Reads the next input record into $0.
 NR: Number of records (lines) read so far.

 NF: Number of fields in the current record.

 FNR: Record number in the current file.

6. Output Functions:
 print items: Prints items to the standard output.

 printf format, items: Prints formatted output.

7. Special Patterns:
 BEGIN: Block of code executed before processing input.

 END: Block of code executed after processing input.

These are just a few examples, and awk provides many more
functions and features. The usage of these functions depends on
the specific requirements of your awk script

let's create simple scenarios using awk functions for string


manipulation: gsub, split, index, and substr. Assume we
have a file named student_info.txt with the following
content:

StudentID,FullName,Grade
1,Alice Smith,A
2,Bob Johnson,B
3,Charlie Williams,C
4,David Miller,B
5,Eva Johnson,A

Now, let's create scenarios:

1. Replacing Spaces in Full Names:


 Objective: Replace spaces in the full names with
underscores.
bashCopy code
awk -F',' '{gsub(/ /, "_", $2); print}' student_info.txt
This awk command uses gsub to globally replace spaces with
underscores in the FullName column.
2. Splitting Full Names into First and Last Names:
 Objective: Split the FullName column into first and last

names.
bashCopy code
awk -F',' '{split($2, names, " "); print $1, "First Name:", names[1], "Last
Name:", names[2]}' student_info.txt
Here, awk uses split to split the FullName into an array names
using a space as the delimiter.
3. Index of Letter 'a' in Full Names:
 Objective: Find the index of the letter 'a' in the FullName

column.
bashCopy code
awk -F',' '{indexResult = index($2, "a"); print $1, "Index of 'a':",
indexResult}' student_info.txt
The awk command uses index to find the position of the letter
'a' in the FullName column.
4. Extracting Substrings from Full Names:
 Objective: Extract the first three characters of each full

name.
bashCopy code
awk -F',' '{substringResult = substr($2, 1, 3); print $1, "First Three
Characters:", substringResult}' student_info.txt
Here, awk utilizes substr to extract the first three characters of
the FullName column.
These examples are designed to be simple and educational,
showcasing the basic usage of gsub for global substitution,
split for splitting strings into arrays, index for finding
positions, and substr for extracting substrings. You can
experiment with these commands and modify them based on
your understanding

Let's create simple scenarios using awk array functions for


practice. Assume we have a file named grades.csv with the
following content:
StudentID,FullName,Math,English,Science
1,Alice Smith,85,90,78
2,Bob Johnson,92,88,95
3,Charlie Williams,78,85,80
4,David Miller,90,92,88
5,Eva Johnson,85,80,92

Now, let's create scenarios:

1. Calculating Total and Average Scores:


 Objective: Calculate the total and average score for each

student.
bashCopy code
awk -F',' 'NR > 1 {total = $3 + $4 + $5; avg = total / 3; print $2, "Total:",
total, "Average:", avg}' grades.csv
This awk command calculates the total and average score for each
student and prints their full name, total score, and average score.
2. Identifying High Scorers:
 Objective: Identify students with an average score above 85.

bashCopy code
awk -F',' 'NR > 1 {total = $3 + $4 + $5; avg = total / 3; if (avg > 85)
print $2, "has a high average score."}' grades.csv
Here, awk identifies and prints the names of students with an
average score above 85.
3. Displaying Students with Math Scores Above 90:
 Objective: Display names of students with Math scores

above 90.
bashCopy code
awk -F',' 'NR > 1 && $3 > 90 {print $2, "has a Math score above 90."}'
grades.csv
This awk command identifies and prints the names of students
with Math scores above 90.
4. Calculating Class Averages:
 Objective: Calculate the class averages for each subject.

bashCopy code
awk -F',' 'NR == 1 {print "Subject ClassAverage"} NR > 1 {mathTotal
+= $3; englishTotal += $4; scienceTotal += $5} END {printf "Math
%.2f\nEnglish %.2f\nScience %.2f\n", mathTotal/(NR-1),
englishTotal/(NR-1), scienceTotal/(NR-1)}' grades.csv
This awk command calculates and prints the class averages for
Math, English, and Science.
5. Creating a Grade Report:
 Objective: Assign grades (A, B, C) based on average scores.

bashCopy code
awk -F',' 'NR == 1 {print "FullName AverageGrade"} NR > 1 {total =
$3 + $4 + $5; avg = total / 3; grade = (avg >= 90) ? "A" : (avg >= 80) ?
"B" : "C"; print $2, avg, grade}' grades.csv
Here, awk assigns grades based on average scores and prints the
full name, average score, and assigned grade.
These examples are designed to practice array-related functions in
awk. T

Let's create a simple scenario using a for loop in awk. Assume


we have a file named numbers.txt with the following content:

5
12
8
17
4

Now, let's create a scenario:

Scenario: Calculating the Sum of Numbers in the File

 Objective: Calculate the sum of the numbers in the file.


bashCopy code
awk '{sum = 0; for (i = 1; i <= NF; i++) sum += $i; print "Sum:", sum}'
numbers.txt

In this awk command:

 NF represents the number of fields in the current record (line).


 The for loop iterates over each field, adding its value to the sum.
 Finally, it prints the total sum.

When you run this command, it will output:

code
Sum: 46
Let's create a simple scenario using getline and FNR in awk.
Assume you have two files named file1.txt and file2.txt
with the following content:

file1.txt:

Apple
Banana
Cherry

file2.txt:

1
2
3

Now, let's create a scenario:

Scenario: Combining Lines from Two Files

 Objective: Combine lines from file1.txt and file2.txt to


create pairs.
bashCopy code
awk 'FNR == NR { fruits[FNR] = $0; next } { print fruits[FNR], $0 }'
file1.txt file2.txt

Explanation:

 FNR == NR ensures that the actions inside the braces {} are only
applied to the first file (file1.txt).
 fruits[FNR] = $0 stores each line of the first file in an array
fruits with the line number as the index.
 next skips the rest of the actions for the current record and
moves to the next record.
 { print fruits[FNR], $0 } prints each line from
file1.txt along with the corresponding line from file2.txt.

When you run this command, it will output:

code
Apple 1 Banana 2 Cherry 3

This example demonstrates how to use getline and FNR to read


lines from two files and combine them. The FNR == NR condition
is a common pattern in awk to distinguish between processing
the first file and subsequent files.

Let's create a simple scenario using BEGIN and END in awk.


Assume you have a file named grades.csv with the following
content:
StudentID,FullName,Math,English,Science
1,Alice Smith,85,90,78
2,Bob Johnson,92,88,95
3,Charlie Williams,78,85,80
4,David Miller,90,92,88
5,Eva Johnson,85,80,92

Now, let's create a scenario:

Scenario: Calculating Class Averages

 Objective: Calculate and display the class averages for each


subject at the end.
bashCopy code
awk -F',' 'NR == 1 {print "Subject ClassAverage"} NR > 1 {mathTotal
+= $3; englishTotal += $4; scienceTotal += $5} END {printf "Math
%.2f\nEnglish %.2f\nScience %.2f\n", mathTotal/(NR-1),
englishTotal/(NR-1), scienceTotal/(NR-1)}' grades.csv

Explanation:

 NR == 1 is a condition that is true only for the first line, which


contains column headers.
 BEGIN is not used in this example, but you can include actions to
be performed before processing the input files.
 The body of the awk command calculates the running totals for
each subject as it processes each line.
 END is used for actions to be performed after processing all input
files or records.
 Inside the END block, it calculates and prints the class averages for
Math, English, and Science.

When you run this command, it will output:

Subject ClassAverage
Math 86.00
English 87.00
Science 86.60

This example demonstrates the use of BEGIN and END in awk for
performing actions at the beginning and end of the processing,
respectively.
Let's create a simple scenario using BEGIN and END in awk.
Assume you have a file named numbers.txt with the following
content:
8
15
3
12
5

Now, let's create a scenario:

Scenario: Finding the Sum of Numbers

 Objective: Find and display the sum of numbers from the file.
bashCopy code
awk 'BEGIN {sum = 0} {sum += $1} END {print "Sum:", sum}'
numbers.txt

Explanation:

 BEGIN is used for actions to be performed before processing


input files.
 The BEGIN block initializes the variable sum to 0.
 For each line in the file, it adds the number to the running sum.
 END is used for actions to be performed after processing all input
files or records.
 Inside the END block, it prints the final sum.

When you run this command, it will output:

makefileCopy code
Sum: 43
This example demonstrates how to use BEGIN and END in awk to
perform actions before and after processing the input files,
respecti

You might also like