From Fedora Project Wiki
Description
This test case aims to validate that gawk, a text processing utility, performs basic and some advanced operations correctly.
Setup
- Install
gawkif not already installed using the command:sudo dnf install gawk
How to test
- Open a terminal window.
- Basic Operations
- Create a text file with some sample numbers. Run
echo -e "1\n2\n3" > numbers.txt. - Run a simple
gawkcommand to sum the numbers in the file:gawk '{s+=$1} END {print s}' numbers.txt - Run a
gawkcommand to print the first field of each line in the file:gawk '{print $1}' numbers.txt
- Working with Text
- Create a text file with some sample text. Run
echo -e "apple orange\nbanana grape" > fruits.txt. - Use
gawkto print only the first fruit from each line:gawk '{print $1}' fruits.txt
- Conditionals and Loops
- Use
gawkto sum only the even numbers in 'numbers.txt':gawk '{if ($1 % 2 == 0) s+=$1} END {print s}' numbers.txt - Use
gawkto print each line from 'fruits.txt' with a line number:gawk '{print NR " " $0}' fruits.txt
- File Processing
- Use
gawkto combine actions; print line number before lines containing 'apple' in 'fruits.txt':gawk '/apple/ {print NR " " $0}' fruits.txt
Expected Results
- The
gawkpackage should install without errors. - The first
gawkcommand should output the sum of the numbers (in this case, "6"). - The second
gawkcommand should print each number on a new line. - The
gawkcommand for printing the first fruit should output "apple" and "banana" on new lines. - The conditional
gawkcommand should correctly sum only the even numbers. - Line numbers should be correctly printed before each line when using the NR variable.
- Only lines containing 'apple' should be printed with a line number before them.
Optional
For additional testing, you may try more complex gawk scripts to validate its functionality.
