How to take awk result out (piping to other program)?

Hi! all here is my code
which is working fine no errors but I want to know how to take result and input to other program

awk 'FNR==1{i++}{LC=NR} 
{for(k=1; k<=NF; k++) A[i,FNR,k]=$k}                                 
END{for (i=1;i<=LC[1];i++)
{
for(j=1;j<=LC[2];j++)
if(A[2,j,8]=='$UID' && A[2,j,4]>='$MX'+A[1,i,3] && A[2,j,4]<='$MIN'+A[1,i,4] && \
A[2,j,4]>='$MN'+A[1,i,1] && A[2,j,5]<='$MX'+A[1,i,2])
print A[2,j,6],A[2,j,7]
}}' OFS="\t" STD_FIILE FILE2 | program(print) command -XA[2,j,5] -Y[2,j,5]   -------------> if statement is true

if if statement is awk program in false

then

OFS="\t" STD_FIILE FILE2 | program(mask) command -XA[2,j,5] -Y[2,j,5]   -------------> 

please help me

if not possible please give me solution in bash

Not understood your requirement.

First thing I would do is make the code more readable for others.

This is how I see the details:

awk 'FNR==1{i++}{LC=NR} \
{\
    for(k=1; k<=NF; k++)\
        A[i,FNR,k]=$k\
}\
END\
{\
    for (i=1;i<=LC[1];i++) {\
        for(j=1;j<=LC[2];j++)\
            If(A[2,j,8]=='$UID'\
                && A[2,j,4]>='$MX'+A[1,i,3]\
                && A[2,j,4]<='$MIN'+A[1,i,4]\
                && A[2,j,4]>='$MN'+A[1,i,1]\
                && A[2,j,5]<='$MX'+A[1,i,2])\
                    print A[2,j,6],A[2,j,7]\
    }\
}'

of the awk command. As this is all before the | (pipe) this is what goes to
whatever is behind the pipe (which I do not understand).

It may be that what you need is an "else" block in your awk program logic to output the "falses". However, noone can see from your code whether you want true/false in a single stream (pipe), or in separate streams.

Someone else could help with setting up different pipes (on different file descriptors) if you want separate streams. For that I would set umask to 0700 and use temporary files for mixed output, and have a trap to catch errors, interrupts and other signals to remove my temporary files.

Hope this helps.

1 Like

can you please elaborate more?

Pipe it out to a file like this inside of an awk code:

awk '{
.....
print > "myfile.txt"
.....
}'

Or, if you want to separate the output to return values for example try marking your statements with specific strings such as

print "RETURN: This is my return message.";

Like others have said as well, use a pipe at the end too:

awk ' BEGIN { ... } { ..... } END { .... }' | while read line; do ... <SOMETHING> ... done;

Cheers,
TK

Something like this ?

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

It all comes down to what kind of program you want to be running based on your output from awk. If you want to do something simple, like one basic command to run for each output line, then you could use the xargs command.

However if the program picking up the results is doing a set of operations based on each output line, then I would also opt for saving the output to a workfile and call your program to load up the file's contents. Calling a complex program X times, where X is the number of lines to process is very ineffective. In this case you should work from a file instead.

Hi Akshay,
I think we're unable to help you because we're unable to figure out what you're trying to do.

We don't know what is in your input files.
We don't know what the values of the shell variables MAX , MIN , MN , and UID are.
We can assume that LC[1] is the number of lines in the 1st input file, but why is LC[2] set to the number of lines in both input files instead of the number of lines in the 2nd input file?
How are:

program(print) command -XA[2,j,5] -Y[2,j,5]
     and
program(mask) command -XA[2,j,5] -Y[2,j,5]

supposed to be processed? (That isn't valid shell command syntax. Is the j in these commands supposed to be related to the j in the 2nd for loop in the END clause in your awk script? Are XA[2,j,5] and Y[2,j,5] supposed to be related to the awk script's array element A[2,j,5] )?
Are you trying to pipe the output of the print statement in your awk script to one command and have the same output piped to a different command if the if statement doesn't print anything? Is it possible that you want something like:

if(complex_condition)
        print A[2,j,6],A[2,j,7] | true_command
else
        print A[2,j,6],A[2,j,7] | false_command

as a replacement for the if statement in the END clause in your awk script?

1 Like