using awk to search and print output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using awk to search and print output
# 1  
Old 09-03-2007
using awk to search and print output

suppose i have one file
file A
18
24
30
35
38
45
55

Another file file B
08_46 A 16 V -0.36 0.23 E : 1.41
08_46 A 17 D -1.04 0.22 E : 0.84
08_46 A 18 Q -0.49 0.12 E : 0.06
08_46 A 19 G 0.50 0.14 E : 0.05
08_46 A 20 V 0.10 0.22 E : 1.26
08_46 A 21 L 0.23 0.22 E : 1.14
08_46 A 22 N -0.86 0.15 E : 0.26
08_46 A 23 N 0.25 0.10 E : 0.18
08_46 A 24 G -0.83 0.20 E : 1.17
08_46 A 25 C 0.39 0.10 E : 0.01
08_46 A 26 F 0.11 0.24 E : 0.54
08_46 A 27 V 0.13 0.15 E : 0.15
08_46 A 28 D 0.23 0.15 B : 0.14
08_46 A 29 A 0.84 0.09 B : 0.54
08_46 A 30 L 0.17 0.16 E : 0.07
08_46 A 31 N 0.36 0.16 E : 0.31
08_46 A 32 V -0.60 0.15 B : 0.08
08_46 A 33 V 0.95 0.07 B : 0.82
08_46 A 34 P 0.59 0.12 E : 0.12
08_46 A 35 H 0.26 0.17 E : 0.15
08_46 A 36 V 0.96 0.09 B : 0.55
08_46 A 37 F 1.01 0.20 E : 0.40
08_46 A 38 L -0.20 0.18 E : 0.50
08_46 A 39 L -0.07 0.18 E : 0.07
08_46 A 40 F 1.05 0.10 B : 0.42
08_46 A 41 I 0.02 0.20 E : 0.37
08_46 A 42 T 0.23 0.16 E : 0.44
08_46 A 43 F 0.12 0.08 B : 0.41
08_46 A 44 P 0.20 0.10 B : 0.56
08_46 A 45 I 0.04 0.23 E : 1.17
08_46 A 46 L -0.21 0.17 E : 0.05
08_46 A 47 F 0.28 0.11 B : 0.30
08_46 A 48 I 0.49 0.23 E : 0.43
08_46 A 49 G 0.15 0.12 B : 0.12
08_46 A 50 W 0.40 0.17 E : 0.09
08_46 A 51 G 0.27 0.14 B : 0.09
08_46 A 52 S 0.17 0.02 B : 1.07
08_46 A 53 Q 0.27 0.08 B : 0.37
08_46 A 54 S 0.01 0.03 B : 0.76
08_46 A 55 S 0.04 0.11 B : 0.45


so we have file B where third column are in common with file A
so the output shud be like
08_46 A 18 Q -0.49 0.12 E : 0.06
08_46 A 30 L 0.17 0.16 E : 0.07
08_46 A 35 H 0.26 0.17 E : 0.15
08_46 A 38 L -0.20 0.18 E : 0.50
08_46 A 45 I 0.04 0.23 E : 1.17
08_46 A 55 S 0.04 0.11 B : 0.45
# 2  
Old 09-03-2007
You missed the line with $3==24.
Code:
awk 'NR==FNR{x[$1];next}$3 in x' fileA fileB

Use nawk or /usr/xpg4/bin/awk on Solaris.

Last edited by radoulov; 09-04-2007 at 07:58 AM.. Reason: correction
# 3  
Old 09-05-2007
MySQL

Quote:
Originally Posted by radoulov
You missed the line with $3==24.
Code:
awk 'NR==FNR{x[$1];next}$3 in x' fileA fileB

Use nawk or /usr/xpg4/bin/awk on Solaris.
Thanks yaar
# 4  
Old 09-05-2007
Try this!!!!!!!!!!!!!!!!

#!/usr/bin/ksh

cat file2 | while read main
do
third=`echo $main | cut -d' ' -f3`
cat file1 | while read inner
do
if [[ "$inner" = "$third" ]]
then
echo "$main"
fi
done
done
# 5  
Old 09-05-2007
U can use join:
Code:
> join -j1 1 -j2 3 fA fB      
18 08_46 A Q -0.49 0.12 E : 0.06
24 08_46 A G -0.83 0.20 E : 1.17
30 08_46 A L 0.17 0.16 E : 0.07
35 08_46 A H 0.26 0.17 E : 0.15
38 08_46 A L -0.20 0.18 E : 0.50
45 08_46 A I 0.04 0.23 E : 1.17
55 08_46 A S 0.04 0.11 B : 0.45

Regards
# 6  
Old 09-18-2007
This one is ok i think.

The a b is the two files:

Code:
awk '{
if (NF==1)
a[$0]=$0
else
for ( j in a)
if ($3==j)
print
}
' a b

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

(n)awk: print regex search output lines in one line

Hello. I have been looking high and low for the solution for this. I seems there should be a simple answer, but alas. I have a big xml file, and I need to extract certain information from specific items. The information I need can be found between a specific set of tags. let's call them... (2 Replies)
Discussion started by: Tobias-Reiper
2 Replies

2. Shell Programming and Scripting

awk script to search output for a value and print

GOODNUMBERS="1 2 3 4 5 6 3 3 34 34 5 66 12" BADNUMBERS="7 3 12 5 66" for eachnum in `echo ${GOODNUMBERS}` do echo ${BADNUMBERS} | gawk -v threshold=${eachnum} '$1 != threshold' done what im trying to do with the above is, i want to print numbers that are in the GOODNUMBERS... (10 Replies)
Discussion started by: SkySmart
10 Replies

3. Shell Programming and Scripting

awk command for complex search and print

Input: 9999~cbc~100~209~ozzxczcz~10001001001~100~abc10 123~a~sdklfjl~sldoi~~~ksjdfnkjsdf~123456 125~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345 127~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~123 128~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345... (7 Replies)
Discussion started by: onesuri
7 Replies

4. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

5. Shell Programming and Scripting

Help with sed/awk for reverse search and print

I have a file which is DFDG START DSFDS DSDS XXX END (VIO) AADD START SDSD FGFG END and I have to print the lines between START and END (VIO). In the files there are multiple places where START would be followed by END with few lines in between but I need to print only if START is... (18 Replies)
Discussion started by: pgbuddy
18 Replies

6. Shell Programming and Scripting

compare two files and search keyword and print output

You have two files to compare by searching keyword from one file into another file File A 23 >pp_ANSWER 24 >aa hello 25 >jau head wear 66 >jss oops 872 >aqq olps ploww oww sss 722 >GG_KILLER ..... large files File B Beta done KILLER John Mayor calix meyers ... (5 Replies)
Discussion started by: cdfd123
5 Replies

7. Shell Programming and Scripting

awk search and print

I have a script where I need to use awk, go through some output, which is stored in a variable, and find a string Xms and Xmx and print the results, including the rest of that string. Example of string: ... (3 Replies)
Discussion started by: cbo0485
3 Replies

8. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address 192.168.1.1 interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies

9. Shell Programming and Scripting

awk search column, print line

Hello. I've been banging my head against walls trying to search a comma delimited file, using awk. I'm trying to search a "column" for a specific parameter, if it matches, then I'd like to print the whole line. I've read in multiple texts: awk -F, '{ if ($4 == "string") print $0 }'... (2 Replies)
Discussion started by: Matthias03
2 Replies

10. Shell Programming and Scripting

Shell script to search through numbers and print the output

Suppose u have a file like 1 30 ABCSAAHSNJQJALBALMKAANKAMLAMALK 4562676268836826826868268468368282972982 2863923792102370179372012792701739729291 31 60... (8 Replies)
Discussion started by: cdfd123
8 Replies
Login or Register to Ask a Question