Awk save ouput command for variable

I need to execute a command on a line of some file. For instance:

file1.txt 100 4
file2.txt 19 8

So my awkscript should execute something like

command $1 $2 $3

and save the output command $1 $2 $3, so system()it wonโ€™t work and it wonโ€™t getline. (I cannot convey the conclusion if I do something like this.)

The limitation of this problem is to use only awk. (I already had a solution with bashscriot + awk ... but I just want awk ... just to find out more about this)

+3
source share
2 answers

What is wrong with using getline?

$ ./test.awk test.txt
# ls -F | grep test
test.awk *
test.txt

# cat test.txt | nl
     1  ls -F | grep test
     2  cat test.txt | nl
     3  cat test.awk

# cat test.awk
#!/usr/bin/awk -f
{
        cmd[NR] = $0
        while ($0 | getline line) output[NR] = output[NR] line RS
}
END {
        for (i in cmd) print "# " cmd[i] ORS output[i]
}
+2

Awk system() /bin/sh, , " > file.out", .

awk '{system("command " $1 " " $2 " " $3 ">" $1 ".out");}'

: ok, save, awk. . , awk getline, backticks $(cmd) shell/perl. , google awk backticks : http://www.omnigroup.com/mailman/archive/macosx-admin/2006-May/054665.html

, getline, . . , /bin/sh bash:

{ "set +o posix; command " $1 " " $2 " "  $3  " | tee >(grep foo)"  | getline var; print toupper(var); } # bash-only, and broken.

set +o posix , awk bash sh, posix . , , bash.

, :


    $ touch foo bar
    $ echo "foo bar" | 
      awk '{ "{ ls " $1 " " $2 " "  $3  " | tee /dev/fd/10 | grep foo > /dev/tty; } 10>&1"  | getline var; print toupper(var); }'
    foo
    BAR
0

Source: https://habr.com/ru/post/1725017/


All Articles