k1r1t0
k1r1t0

Reputation: 767

Multiple pipelines in AWK

I want to do something like this:

'{ 
    var = "value";
    "echo -n "$var"|"xxd -r -p"|"sha256sum"|"cut -d \" \" -f 1"|getline var;
}'

I guess in awk I can't use piplines this way, so how can this be implemented in awk?

Upvotes: 0

Views: 144

Answers (2)

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2821

tested and confirmed working on mawk, gawk, and nawk

=

 <( … input_from_/dev/stdin … )> |

 mawk '
 function ______(__,_) {
 1     _ = "\47"
 1     gsub(_, "&\\" (_)_,__)

 1     return (_)(__)_
   }

 function _______(__,_,___) {
 1     return \
       substr( (___ = RS)*(RS = "\n"), 
        (_=" printf \47%s\47 " (______(__))" | xxd -p " \
           " | xxd -p -r | xxh128sum | awk NF=1") | getline __, 
                           \
       close(_)*_*(RS = ___))__
 }

 {
 1     print $+(_="")

 1     printf("\n xxhash-128-from-_INPUT_ :: ")

 1     printf("%s", $-_) | (__="xxh128sum ")
 1                    close(__)

 1     _ = _______($-_)

 1     print "\n xxhash-128-from-getline "\
                       ":: getline :: [[ "(_)" ]]\n" } '

—- it's one-single line, but reformatted for readability

The quick brown fox !"#$%&'()*+,-./0123456789:;<=>
?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnop
qrstuvwxyz{|}~jumps over the lazy !"#$%&'()*+,-./0
123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ab
cdefghijklmnopqrstuvwxyz{|}~dog

 xxhash-128-from-_INPUT_ :: 5a43bf3eab449ca21b817f0baeee2a4b  stdin

 xxhash-128-from-getline :: getline :: [[ 5a43bf3eab449ca21b817f0baeee2a4b ]]

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203655

'{ 
    var = "value"
    cmd = "echo -n \047" var "\047 | xxd -r -p | sha256sum | cut -d \" \" -f 1"
    var = ( (cmd | getline line) > 0 ? line : "Failed: " var )
    close(cmd)
    print var
}'

but it's extremely unlikely that this is the best way to do whatever it is you're trying to do. I'm setting var to "Failed: " var if the pipeline to getline fails, you'll have to decide how you really want to handle it. See http://awk.freeshell.org/AllAboutGetline for when/how to use getline and it's many caveats.

Upvotes: 1

Related Questions