compare two files and search keyword and print output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare two files and search keyword and print output
# 1  
Old 12-03-2010
compare two files and search keyword and print output

[CODE]
You have two files to compare by searching keyword from one file into another file

File A
23 >pp_ANSWER
24 >aa hello [Beta done ]
25 >jau head wear [John mayor]
66 >jss oops [calix meyers]
872 >aqq olps ploww oww sss [Mutton deer]
722 >GG_KILLER
..... large files

File B

Beta done
KILLER
John Mayor
calix meyers

Desired output
24 >aa hello [Beta done ]
25 >jau head wear [John mayor]
66 >jss oops [calix meyers]
722 >GG_KILLER

Code:
awk 'NR==FNR{a[$1]=$1;next}$0 in a{print $1,a[$1],$2}'
thought like any keyword will match will come but not working

# 2  
Old 12-03-2010
Code:
>outputFile
cat FileB | while read line
do
grep "$line" FileA >> outputFile
done

This User Gave Thanks to For This Post:
R0H0N
# 3  
Old 12-03-2010
Code:
grep -f fileB fileA

This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 12-03-2010
This is just
Code:
grep -f FileB FileA > outputFile

Some implementations may use fgrep instead though, depending what flags are supported.

The code in the previous post could be very slow depending on the data volumes, however it could be adapted to include the square brackets or force other conditions on the search for the string. It depends what you are actually after.





I hope that this helps. Let us all know if I have missed the point.


Robin
Liverpool/Blackburn
UK
# 5  
Old 12-03-2010
Quote:
Originally Posted by rbatte1
This is just
Code:
grep -f FileB FileA > outputFile

Some implementations may use fgrep instead though, depending what flags are supported.

The code in the previous post could be very slow depending on the data volumes, however it could be adapted to include the square brackets or force other conditions on the search for the string. It depends what you are actually after.


U r Right
fgrep is faster here while running this compared as comapred to grep
Thanks


I hope that this helps. Let us all know if I have missed the point.


Robin
Liverpool/Blackburn
UK
---------- Post updated at 06:13 AM ---------- Previous update was at 06:12 AM ----------

Quote:
Originally Posted by rbatte1
This is just
Code:
grep -f FileB FileA > outputFile

Some implementations may use fgrep instead though, depending what flags are supported.

The code in the previous post could be very slow depending on the data volumes, however it could be adapted to include the square brackets or force other conditions on the search for the string. It depends what you are actually after.





I hope that this helps. Let us all know if I have missed the point.


Robin
Liverpool/Blackburn
UK
U r Right
fgrep is faster here while running this compared as comapred to grep
Thanks
# 6  
Old 12-03-2010
Code:
perl -ne 'BEGIN{open (F,"FileA");@f=; } chomp($_);foreach $k (@f){chomp($k); print "\n$k" if ($k=~ m/$_/);}' FileB

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files and print output

Hi All, i am trying to compare two files in Centos 6. F1: /tmp/d21 NAME="xvda" TYPE="disk" SIZE="40G" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="" NAME="xvda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="/boot" NAME="xvda2" TYPE="part"... (2 Replies)
Discussion started by: balu1234
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

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

4. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

5. Shell Programming and Scripting

Keyword search/replace for two text files?

What is the best way (bash/awk/sed?) to read in two text files and do a keyword search/replace? file1.txt: San Francisco Los Angeles Seattle Dallas file2.txt: I love Los Angeles. Coming to Dallas was the right choice. San Francisco is fun. Go to Seattle in the summer. ... (3 Replies)
Discussion started by: pxalpine
3 Replies

6. UNIX for Advanced & Expert Users

Shell Script to compare xml files and print output to a file

All, PLease can you help me with a shell script which can compare two xml files and print the difference to a output file. I have attached one such file for you reference. <Group> <Member ID=":Year_Quad:41501" childCount="4" fullPath="PEPSICO Year-Quad-Wk : FOLDER.52 Weeks Ending Dec... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

7. Shell Programming and Scripting

awk to compare flat files and print output to another file

Hello, I am strugling from quite a some time to compare flat files with over 1 million records could anyone please help me. I want to compare two pipe delimited flat files, file1 with file2 and output the unmatched rows from file2 in file3 Sample File1: ... (9 Replies)
Discussion started by: suhaeb
9 Replies

8. Shell Programming and Scripting

compare columns from seven files and print the output

Hi guys, I need some help to come out with a solution . I have seven such files but I am showing only three for convenience. filea a5 20 a8 16 fileb a3 42 a7 14 filec a5 23 a3 07 The output file shoud contain the data in table form showing first field of... (7 Replies)
Discussion started by: smriti_shridhar
7 Replies

9. Shell Programming and Scripting

awk to compare lines of two files and print output on screen

hey guys, I have two files both with two columns, I have already created an awk code to ignore certain lines (e.g lines that start with 963) as they wou ld begin with a certain string, however, the rest I have added together and calculated the average. At the moment the code also displays... (3 Replies)
Discussion started by: chlfc
3 Replies

10. Shell Programming and Scripting

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 ... (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question