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


 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support How to take awk result out (piping to other program)?
# 1  
Old 03-19-2013
[SOLVED]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

Code:
awk 'FNR==1{i++}{LC[i]=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

Code:
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

Last edited by Akshay Hegde; 01-05-2014 at 02:12 PM..
# 2  
Old 03-20-2013
Not understood your requirement.
# 3  
Old 03-20-2013
First thing I would do is make the code more readable for others.

This is how I see the details:
Code:
awk 'FNR==1{i++}{LC[i]=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.
This User Gave Thanks to MichaelFelt For This Post:
# 4  
Old 08-06-2013
can you please elaborate more?
# 5  
Old 08-16-2013
Pipe it out to a file like this inside of an awk code:

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

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

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

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

Cheers,
TK

Last edited by Scott; 08-16-2013 at 12:03 PM.. Reason: Please use code tags
# 6  
Old 09-04-2013
Something like this ?


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

# 7  
Old 12-09-2013
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tweak python program to give result in json

Hi , Below is the script which prints result in json but when i validate it has some tab or extra space issues. JSON result { "data": } This is the line I tweaked. Please advise. print "\t{", "\"{#NAME}\":\""+container+hn+"\"}" #!/usr/bin/env python # (2 Replies)
Discussion started by: ashokvpp
2 Replies

2. Shell Programming and Scripting

Piping through grep/awk prevents file write

So, this is weird... I'm running this command: iotop -o -P -k -bt -d 5 I'd like to save the output relelvant to rsyslogd to a file, so I do this: iotop -o -P -k -bt -d 5 | grep rsyslogd >> /var/log/rsyslogd Nothing is written to the file! I can write the full output to the file: ... (2 Replies)
Discussion started by: treesloth
2 Replies

3. UNIX for Dummies Questions & Answers

Weird: unexpected result after piping a sort

Hello, And when you think you know the basics of something, UNIX in this case, something like what I will describe below comes along.... On a Linux system, a "typical" directory with some files. Say 20. I do: > ls | sort > mylisting Now when I: > vi mylisting There is mylisting... (13 Replies)
Discussion started by: stavros
13 Replies

4. Shell Programming and Scripting

piping from grep to awk without intermediate files

I am trying to extract the file names alone, for example "TVLI_STATS_NRT_XLSTWS03_20120215_132629.csv", from below output which was given by the grep. sam:/data/log: grep "C10_Subscribe.000|subscribe|newfile|" PDEWG511_TVLI_JOB_STATS.ksh.201202* Output: ... (6 Replies)
Discussion started by: siteregsam
6 Replies

5. Shell Programming and Scripting

Executing the result of a program as a shell script

I have a program that returns a shell script and I want to execute the script. I'll use cat in my simple example, but wget is an example that is feasible. $ # First setup a script $ echo "ls > df" > simple $ # "cat simple" is now a program that returns a script $ cat simple ls df $ ... (3 Replies)
Discussion started by: kopite
3 Replies

6. Programming

Test program not giving expected result

I have five classes. 2 composition classes,1 aggregation class and 1 dependency class.I have coded all the classes but one of my test program is not giving me the expected result.I have the following classes: TimeStamp Interval (composition of 2 TimeStamps) TimeSheet ( aggregation of many... (3 Replies)
Discussion started by: moraks007
3 Replies

7. Shell Programming and Scripting

sort piping to awk array - help please

Hi I'm just learning programming and need some help. I've taken a data file which has a list of numbers eg: 3 5 32 533 13 2 And I've used sort -n and to sort and then piped it to awk to arrange into an array. #!/bin/sh sort -n data.txt | awk ' { array=$1 } (4 Replies)
Discussion started by: EL_Chemso
4 Replies

8. Shell Programming and Scripting

Way to save output result of a program into another new file...

Does anybody know any alternative way to save output result of a program into another new file? I got try the command below: program_used input_file > new_output_file program_used input_file >> new_output_file Unfortunately, both the ">" and ">>" is not work at this case to save the output... (6 Replies)
Discussion started by: patrick87
6 Replies

9. UNIX for Dummies Questions & Answers

Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code: echo... (4 Replies)
Discussion started by: lazypeterson
4 Replies

10. Programming

Help with piping program

Hi, I am trying to write a program that will pipe any number of programs together like in the linux shell. As an example, the below code tries to execute "cat data | grep int | cut -b 1-10." The problem is that the programs never get executed for some reason. It seems like the first program... (3 Replies)
Discussion started by: PuppyHusher
3 Replies
Login or Register to Ask a Question