Skip to main content

Awk

Struct Awk 

Source
pub struct Awk { /* private fields */ }
Expand description

High-level wrapper for compiling and running an AWK script.

The script is parsed once on construction and can then be executed against any number of independent inputs without re-parsing.

§Examples

Print every line of input unchanged:

use rawk_core::awk::Awk;

let awk = Awk::new("{ print }").unwrap();
let (output, error) = awk.run(vec!["hello world".into()], None, None);
assert_eq!(output, vec!["hello world".to_string()]);
assert!(error.is_none());

Supply a filename so that the FILENAME built-in variable is populated:

use rawk_core::awk::Awk;

let awk = Awk::new("{ print FILENAME }").unwrap();
let (output, error) = awk.run(vec!["ignored".into()], Some("data/input.txt".into()), None);
assert_eq!(output, vec!["data/input.txt".to_string()]);
assert!(error.is_none());

Use a custom field separator to parse CSV-style input:

use rawk_core::awk::Awk;

let awk = Awk::new("{ print $1 }").unwrap();
let (output, error) = awk.run(vec!["Alice,30,engineer".into()], None, Some(",".into()));
assert_eq!(output, vec!["Alice".to_string()]);
assert!(error.is_none());

Implementations§

Source§

impl Awk

Source

pub fn new(script: impl Into<String>) -> Result<Self, ParseError<'static>>

Parse an AWK script into an executable program.

The script is stored with a static lifetime to keep the AST valid. Returns a parse error if the script is not valid AWK according to this parser.

Source

pub fn run( &self, input: Vec<String>, filename: Option<String>, field_separator: Option<String>, ) -> (Vec<String>, Option<String>)

Execute the compiled program against the given input lines.

  • filename — exposed as the FILENAME built-in variable inside the script. Pass None to use the default value "-" (conventional stdin placeholder).
  • field_separator — overrides the FS built-in variable used to split each input record into fields ($1, $2, …). Pass None to use the default " ", which splits on runs of whitespace.

Returns (output_lines, runtime_error). When a runtime error occurs, output collected before the error is still returned alongside the error message.

Auto Trait Implementations§

§

impl Freeze for Awk

§

impl RefUnwindSafe for Awk

§

impl Send for Awk

§

impl Sync for Awk

§

impl Unpin for Awk

§

impl UnsafeUnpin for Awk

§

impl UnwindSafe for Awk

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.