English 中文(简体)
awk save command ouput to variable
原标题:

I need to execute a command per line of some file. For example:

file1.txt 100 4
file2.txt 19 8

So my awk script need to execute something like

command $1 $2 $3

and save the output of command $1 $2 $3, so system() will not work and neither will getline. (I can t pipe the output if I do something like this.)

The restriction to this problem is to use only awk. (i already had a solution with bashscriot + awk...but I only want awk...just to know more about this)

最佳回答

What s 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]
}
问题回答

Awk s system() function passes the string to /bin/sh, so you can use redirect operators, like ">file.out" if you want.

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

Edit: ok, by save, you mean into an awk variable. ephemient is on the right track, then. That s what awk s getline does, like backticks or $(cmd) in shell/perl. In fact, google for awk backticks found this: http://www.omnigroup.com/mailman/archive/macosx-admin/2006-May/054665.html

You say you can t use getline because then you couldn t pipe. But you can work around that with tee and file-descriptor tricks. This works if /bin/sh is bash:

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

set +o posix is necessary because awk runs bash as sh, which makes it go into posix mode after readings its startup files. Hmm, I m not having any luck getting that to work, and it requires bash anyway.

Ok, this works:


    $ 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




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签