help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Write to and read from file descriptors in awk - awk: cannot open "/


From: david kerns
Subject: Re: Write to and read from file descriptors in awk - awk: cannot open "/dev/fd/10" for output (No such file or directory)
Date: Sat, 29 Jul 2023 11:51:58 -0700

On Sat, Jul 29, 2023 at 6:15 AM Ahmad Ismail <ismail783@gmail.com> wrote:

> The data source (data.adoc) looks like:
>
> Box Beast Base
> > Bravo Beast Base
> > Brazil Beast Base
> > Bronx Beast Base
>
>
> I have a filter (`./filter`) like:
>
> #!/bin/bash
>
> while
> > read line
> > do
> > echo $line | tr "B" "b"
> > done
>
>
> The following client using coproc works:
>
>  #!/usr/bin/env bash
> > coproc ./filter
>
> exec 10>&"${COPROC[1]}"
> > exec 11<&"${COPROC[0]}"
> > while IFS= read -r line || [ -n "$line" ]; do
> >     echo "$line" >&10
> >     read -r result <&11
> >     echo "${result}"
> > done < <(cat data.adoc)
>
>
> But when I try to use awk replacing the while loop like:
>
> awk '{ print $0 > "/dev/fd/10"
> >     getline result < "/dev/fd/11"
> >     print result }' data.adoc
>
>
> It throws error:
>
> > awk: cannot open "/dev/fd/10" for output (No such file or directory)
>
>
> *Thanks and Best Regards,Ahmad Ismail*
>

The "standard" way in awk is to use a two-pipe:

$ gawk '
BEGIN {
  cmd = "tr \"B\" \"b\""
}
  { print $0 |& cmd }
END {
  close(cmd, "to");
  while ((cmd |& getline x) > 0) {
    print x
  }
}' data.adoc
box beast base
> bravo beast base
> brazil beast base
> bronx beast base


reply via email to

[Prev in Thread] Current Thread [Next in Thread]