0

I process a file content with awk. I would like to pipe the output of awk into netcat, that communicates with a socket.

The awk script is very simple, it just prints $1.

When I pipe the result of awk into netcat, I have no result printed (except the value of addresses_file).

echo "$addresses_file"
echo $(awk -f http-awk-check.awk $addresses_file | netcat -U /home/hduser/socket/rtop12)

Output

../keys/addresses-2021-01-26-17-44.txt

However, I have a result printed when I try with one line by command line:

$ echo 'info' | netcat -U /home/hduser/socket/rtop12
{"jsonrpc":"2.0","id":1,"result":"0x0"}

The awk script:

BEGIN {
    FS = ","
}
{
    print $1 "\n" # same result with and without \n
}

How can I get a result similar to the manual command line with my little script ?

Itération 122442
  • 2,644
  • 2
  • 27
  • 73
  • Is the variable `$addresses_file` being exported? – tink Jan 29 '21 at 18:00
  • @tink It actualy is set when calling the script. And yes it has the correct value. I'll update the question. – Itération 122442 Jan 29 '21 at 18:01
  • So ... the edited output ... is that the first or the second echo? :) – tink Jan 29 '21 at 18:08
  • It's not directly related to your problem, but this needs more quotes to be correct. If you run `echo $(somecommand)`, and `somecommand` emits `*`, you'll print a list of filenames instead of a `*` unless you make it `echo "$(somecommand)"`. This is the same root cause as [I just assigned a variable, but `echo $varaible` prints something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Jan 29 '21 at 18:08
  • 1
    http://shellcheck.net/ is your friend on catching that kind of thing. – Charles Duffy Jan 29 '21 at 18:10
  • @tink, ...why would whether it's exported matter? I don't see it being called from a non-subshell subprocess (and a proper subshell being created by `fork()`ing its parent with no `exec`, it inherits all the parent's shell variables, whether they're in the environment or not). – Charles Duffy Jan 29 '21 at 18:13
  • 1
    Please share the sample content from $addresses_file. As a sanity check, could you try a file having a single line "info" to check whether it matches with the output as per example you have shown in your example? – Kishan Parekh Jan 29 '21 at 18:15

1 Answers1

0

I must have made a mistake somewhere during my tests.

With the following, it works like a charm (but the netcat connexion is not closed thougH...)

BEGIN {
    FS = ","
}
{
    print $1
}

awk -f http-awk-check.awk $addresses_file | netcat -U /home/hduser/socket/rtop12 > toto
Itération 122442
  • 2,644
  • 2
  • 27
  • 73