For Programmers: Free Programming Magazines  


Home > Archive > AWK > June 2004 > Utilizing a previous search output for additional search









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Utilizing a previous search output for additional search
groblela

2004-06-29, 3:56 pm

I'm not sure if this is possible, but I'd like to utilize the output from a
previous search for another search.

The initial search searches column 5 to match a string and prints the
first occurrence (including line number)to an output file.
awk '$5 ~ /string/{print NR;$0;exit}' inputFile > outputFile

Next, I'd like to search the original input file to match the string
occurring in $3 of the last line added to the output file. This line will
then be appended >> to the output file.

I'm attempting to create a timeline of events from the input file. Each
additional search will be conditional based on the previous search
outcome.

...any attempt to validate or point me in the correct direction would be
appreciated.
groblela

Doug McClure

2004-06-29, 3:56 pm

This is untested code. I use TAWK which has an fopen() command that
would allow me to open a file name. I'm not sure that gawk has this
capability. Even if this doesn't work, I think you will get the idea.

Also I don't know how to terminate the program -- you don't indicate a
termination condition.

If your input file was fairly small, I would just read it into an
array and keep (re)processing the data in the array, not in the file.

My program is called as

"awk -f myprog searchstring inputfilename >outputfilename"


BEGIN {

string = ARGV[1]
ARGV[1] = ""

filename = ARGV[2]
ARGV[2] = ""

while ((getline < filename) > 0) {
linenum++

if ($5 ~ /string/)
{
print linenum, $0
string = $3
close(filename)
linenum = 0
}
}
}

DKM


On Tue, 29 Jun 2004 13:12:22 -0400, "groblela"
<lee.grobleski@nospam.wpafb.af.mil> wrote:

>I'm not sure if this is possible, but I'd like to utilize the output from a
>previous search for another search.
>
>The initial search searches column 5 to match a string and prints the
>first occurrence (including line number)to an output file.
>awk '$5 ~ /string/{print NR;$0;exit}' inputFile > outputFile
>
>Next, I'd like to search the original input file to match the string
>occurring in $3 of the last line added to the output file. This line will
>then be appended >> to the output file.
>
>I'm attempting to create a timeline of events from the input file. Each
>additional search will be conditional based on the previous search
>outcome.
>
>..any attempt to validate or point me in the correct direction would be
>appreciated.
>groblela



To contact me directly, send EMAIL to (single letters all)
DEE_KAY_EMM AT EarthLink.net. [For example X_X_X@EarthLink.net.]
Ed Morton

2004-06-29, 3:56 pm



groblela wrote:
> I'm not sure if this is possible, but I'd like to utilize the output from a
> previous search for another search.
>
> The initial search searches column 5 to match a string and prints the
> first occurrence (including line number)to an output file.
> awk '$5 ~ /string/{print NR;$0;exit}' inputFile > outputFile
>
> Next, I'd like to search the original input file to match the string
> occurring in $3 of the last line added to the output file. This line will
> then be appended >> to the output file.
>
> I'm attempting to create a timeline of events from the input file. Each
> additional search will be conditional based on the previous search
> outcome.
>
> ..any attempt to validate or point me in the correct direction would be
> appreciated.
> groblela
>


Try this (untested):

awk 'NR == FNR && pat == "" && $5 ~ /string/ { print NR, $0; pat = $3 }
NR != FNR && pat != "" && $0 ~ pat' inputFile inputFile > outputFile

Regards,

Ed.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com