Call python script from awk

most of the solutions there call awk from python. But I want to do it the other way around. I have a python script that extracts information from a file. However, the specified file name is referenced in the awk script column.

How to pass python argument "% s20s", file name and get input from standard output? I want to add output as few columns more.

thanks for your examples

Greetings

+3
source share
2 answers

The awk variable FILENAME indicates the name of the file being processed (or '-' if stdin). However, this is not possible in the BEGIN block, but instead you can use ARGV [1] (assuming you only pass one file name):

#!/bin/awk -f

BEGIN {
    cmd = "./myscript.py '\"%s20s\"' " ARGV[1]
    print cmd
    cmd  | getline var       
    print var
}

The python script (py3) that I used for testing was:

#!/usr/bin/python

import sys
print(sys.argv)

, :

/home/user1> runit.awk afile
./myscript.py "%s20s" afile
['./myscript.py', '"%s20s"', 'afile']
+3

system. ?

$ awk 'BEGIN { system("echo something") }'
something

. stdin, :

$ awk 'BEGIN { "echo something" | getline; print "output: "$0 }'
output: something

Getline , , :

$ awk 'BEGIN { while ("cat multi_line_file" | getline) { print "output: "$0 } }'
output: line 1
output: line 2
output: line 3
+3

All Articles