Skip to content

rwaskiewicz/rawk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

r-awk

a toy awk written in Rust, feature incomplete.

Minimum Rust Version

The minimum version required to compile and run r-awk can be found under the rust-version key in the project's Cargo.toml file.

Compiling

cargo build --release

Running

At this time, running a REPL is fully supported and has limited support for awk file reading. Like a real awk, this r-awk will take a single program from the STDIN when invoked. It then will prompt for input to serve as the data that is fed into the program.

The program below demonstrates running a compiled r-awk and demonstrates the usage of field variables and field separators.

./rawk -F, '{print $2 * $3 + $1;}'
1,2,3
7
4,5,6
34

Patterns are supported:

./rawk -F, '$1 > $2 {print "First is bigger";} $2 > $1 {print "Second is bigger";}'
1,2
Second is bigger
2,1
First is bigger

Multi-line programs are supported in the REPL. Take 'fizzbuzz' for example:

./rawk '{
    for (i=0; i<=100; i=i+1) {
      result = "";
      if (i % 3 == 0) { 
        result = "fizz";
      }
      if (i % 5 == 0) {
        result = result "buzz";
      }
      if (result == "") {
        result = i;
      }
      print result;
    }
}'

Reading an awk program from a file:

./rawk -f ./awk_examples/field_variables/it_prints_all_line_parts.awk
IN: alice 40 25
OUT: alice 40 25

Reading an awk program and data from a file:

./rawk -f ./awk_examples/field_variables/it_prints_all_line_parts.awk ./tests/data/hours1.dat
Alice 25.00 10
Bob 20.75 20
Charlie 15.25 40
Dan 21.50 0
Erin 22.00 30

Logging

The env_logger crate is used as the implementation behind the log facade. Instructions for configuring log levels can be found in the crate's documentation.

By default, the REPL will run with the 'Info' log filter.

Example usage:

RUST_LOG=debug cargo run -- '{print $2 * $3 + $1;}'

References

About

a toy awk implementation

Resources

Stars

Watchers

Forks

Releases

No releases published