1

What I'm trying to do

I'm trying to apply a substitution with sed on an output of the rust compiler.

Small example

Do cargo new test.

src/main.rs

fn main() {
    println("Println is a macro!!");
}

Now do cargo run | sed -n -e 's/help/Help/'.

Expected output

error[E0423]: expected function, found macro `println`
 --> src/main.rs:2:5
  |
2 |     println("Println is a macro!!");
  |     ^^^^^^^ not a function
  |
Help: use `!` to invoke the macro
  |
2 |     println!("Println is a macro!!");
  |            +

For more information about this error, try `rustc --explain E0423`.
error: could not compile `rust_tmp` due to previous error

What I get

error[E0423]: expected function, found macro `println`
 --> src/main.rs:2:5
  |
2 |     println("Println is a macro!!");
  |     ^^^^^^^ not a function
  |
help: use `!` to invoke the macro
  |
2 |     println!("Println is a macro!!");
  |            +

For more information about this error, try `rustc --explain E0423`.
error: could not compile `rust_tmp` due to previous error

(nothing has changed)

What else I tried

I also tried:

  • cargo run 2>&1 sed -n -e 's/help/Help/'
TornaxO7
  • 1,150
  • 9
  • 24
  • 1
    Your last try is a step in the right direction; the output you see from Rust's compiler is written on `stderr` rather than `stdout`. This isn't specific to Rust. You can see this question: https://stackoverflow.com/questions/2342826/how-can-i-pipe-stderr-and-not-stdout – Vincent Savard Jun 06 '22 at 13:12

1 Answers1

3

Try piping stderr instead of stdout to sed: cargo run 3>&1 1>&2 2>&3 | sed -n -e 's/help/Help/'. You can also pipe both stderr & stdout to sed: cargo run |& sed -n -e 's/help/Help/'.

Have a look at the bash manual chapter on redirections.