AWK Functions
AWK Functions
String Functions:
length(string): Returns the length of the string.
and 1.
sin(x), cos(x), atan2(y, x): Trigonometric functions.
3. Array Functions:
delete array[index]: Deletes an element from an array.
array.
for (var in array): Iterates over the elements of an
array.
4. Control Flow Functions:
if (condition) { statements } else {
6. Output Functions:
print items: Prints items to the standard output.
7. Special Patterns:
BEGIN: Block of code executed before 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
StudentID,FullName,Grade
1,Alice Smith,A
2,Bob Johnson,B
3,Charlie Williams,C
4,David Miller,B
5,Eva Johnson,A
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
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
5
12
8
17
4
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
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.
code
Apple 1 Banana 2 Cherry 3
Explanation:
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
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:
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