Operating System - HP-UX
1833771 Members
2225 Online
110063 Solutions
New Discussion

Re: getting output using awk

 
SOLVED
Go to solution
Florinda Adato
Occasional Advisor

getting output using awk

Hi,

I have an awk script that actually parse the fields in the file but my problem is, I must get/grep the field value in another file.
My script looks like this:

awk '{FIELD1=$1; FIELD2=$2;
printf ("%15s %10s", FIELD1, FIELD2)}' FILE1

I wanted to get/grep the FIELD2 from FILE2 in order to get another data from FILE2, how do I get it?
14 REPLIES 14
Jean-Luc Oudart
Honored Contributor

Re: getting output using awk

Within your awk script you can also tun a grep command:

s1=sprintf(" grep %s FILE2",FIELD2);
system(s1);

Note for each FIELD2 it will generate a process grep FILE2.

Rgds,
Jean-Luc
fiat lux
Florinda Adato
Occasional Advisor

Re: getting output using awk

Thanks. But how will I save the output of my grep to another variable?
Rodney Hills
Honored Contributor

Re: getting output using awk

How about using "join" to combine the fields you want from file1 and file2.

Example-
join -j1 1 -j2 1 -o 1.1,2.2 | awk ...

This would send the common key field for file1 and file2 and the second field of file2 to awk.

"join" works like a SQL join, treating the 2 text files like tables.

HTH

-- Rod Hills
There be dragons...
Florinda Adato
Occasional Advisor

Re: getting output using awk

Thanks.

Join works fine but I need find the matched
of field2 from file2 using awk.

My FILE1 have these fields:
12345 20
12346 30

My FILE2 have these fields:
20 hello
30 thanks

What I want is to get the string "hello" or "thanks" in my awk.

The output will be something like these:
12345 20 hello
12346 30 thanks
Hoefnix
Honored Contributor

Re: getting output using awk

try this one(if the files are very big it will take a while because it running through file1 for each entry in file2:

cat file2 | while read indx word
do
indx2=""
cat file1 | while read field1 indx2
do
if [ $indx -eq $indx2 ]
then
echo "$field1 $indx $word"
fi
done
done

file1:
12345 20
12346 30

file2
20 hello
30 thanks


Output:
12345 20 hello
12346 30 thanks

Regards,

Peter
Florinda Adato
Occasional Advisor

Re: getting output using awk

My file is about 700k lines, I need something faster.
Jeroen Peereboom
Honored Contributor

Re: getting output using awk

Are both files large?
If one is small, it may be possible to fill an array with the values and then process each line of the large file, looking up the value in the array.

If both are large, sort both files on the field they have in common. Then read both files, advancing in a file if the common field is less than the field of the current line of the other file.

This may be tricky in awk.

JP.
Elmar P. Kolkman
Honored Contributor

Re: getting output using awk

(
cat FILE2
echo --- starting data ---
cat FILE1
) awk '/--- starting data ---/ {infile1=1}
infile1==0 { response[$1]=$2 }
infile1==1 { printf("%15s %10s %s\n",$1,$2,response[$2]);}'

This should do the trick...
Every problem has at least one solution. Only some solutions are harder to find.
Jeroen Peereboom
Honored Contributor

Re: getting output using awk

Elamr,

looks nice. Wonder what Florinda thinks (and awards?)

JP.
Elmar P. Kolkman
Honored Contributor
Solution

Re: getting output using awk

Jeroen, Nice to see you online... ;-) Long time no see. While I'm back at the company we worked together in the past...

Florinda, if you check my solutions, put a pipe ('|') between the closing bracket ('}') and awk, otherwise it won't work (and complain about a syntax error).
Every problem has at least one solution. Only some solutions are harder to find.
Florinda Adato
Occasional Advisor

Re: getting output using awk

thank you very much, until next time :-)
Florinda Adato
Occasional Advisor

Re: getting output using awk

Hello.

How about if my files are something like these:

::::::::::::::
FILE1
::::::::::::::
12345 123 123 20
12346 123 123 30
12347 123 123 20
12348 123 123 90
::::::::::::::
FILE2
::::::::::::::
20 hello
30 thanks


OUTPUT IS:
12345 123 123 20 hello
12346 123 123 30 thanks
12347 123 123 20 hello
12348 123 123 90

Sorry I'm very new in using awk.

Thanks
Elmar P. Kolkman
Honored Contributor

Re: getting output using awk

You can use the same script I sent earlier, but replace the line
'infile1==1 { printf("%15s %10s %s\n",$1,$2,response[$2]);}'

with

'infile1==1 { printf("%s %s\n",$0,response[$NF]);}'

This will work, no matter how many fields are in FILE1
Every problem has at least one solution. Only some solutions are harder to find.
Marc Roger
Advisor

Re: getting output using awk

The same in perl, and it's probably faster.

#!/usr/contrib/bin/perl

open(FILE2, $ARGV[1]);
while() {
($field1, $field2)=split;
$map{$field1}=$field2;
}
close(FILE2);

open(FILE1, $ARGV[0]);
while() {
chop;
@l=split;
print "$_ $map{$l[$#l]}\n";
}
close(FILE1);