home ~ projects ~ socials

Send A String To awk For Processing In Rust

TODO: Update this example to be a full example

fn send_to_awk(input: &str) -> Result<String, Error> {
  let args = vec!["/crop/ { print $NF }"];
  let mut cmd = Command::new("awk")
    .args(args)
    .stdin(Stdio::piped())
    .stdout(Stdio::piped())
    .spawn()?;
  let cmd_stdin = cmd.stdin.as_mut().unwrap();
  cmd_stdin.write_all(input.as_bytes())?;
  let output = cmd.wait_with_output()?;
  let response = String::from_utf8(output.stdout).unwrap();
  Ok(response.lines().next().unwrap().to_string())
}
-- end of line --