find keyword from file and search in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find keyword from file and search in another file
# 1  
Old 12-28-2010
Power find keyword from file and search in another file

hi dudes;

this is my file.txt:
Code:
20101228-180436_Down
a	1
b	2
...
20101228-190436_Rollback
a	1	40
e	3	20
...
20101228-180436_Down
c	2
f	2
c	1
...

and i have a down.txt:
Code:
a	1	aa	2	30	bb	1	40
b	2	ab	3	10
c	3	cd	4	50	ac	2	20
c	3	ad	1	0
d	1	af	2	10	av	3	70	ax	4	20
c	2
f	2	bt	5	20
c	1
g	3	ah	4	20	av	1	10	az	5	10
...


i want to search for down portnames in file.txt and get results from down.txt matching first two columns of it. so the result.txt must be:
Code:
20101228-180436_Down
a	1	aa	2	30	bb	1	40
b	2	ab	3	10
...
20101228-190436_Rollback
a	1	40
e	3	20
...
20101228-180436_Down
c	2
f	2	bt	5	20
c	1
...

please keep in mind that; a 1 is in both down and rollback list. so i cannot search file.txt line by line. i have to take ONLY down ports and write results next to ONLY down port list.

Last edited by gc_sw; 12-28-2010 at 11:29 AM..
# 2  
Old 12-28-2010
Code:
awk 'NR==FNR{a[$1_$2]=$0;next;}{if(NF==1){if(substr($1,length($1)-3)=="Down") b=1; else b=0; } if(b==1 && a[$1_$2]) $0=a[$1_$2]; print; }' down.txt file.txt

This User Gave Thanks to anurag.singh For This Post:
# 3  
Old 12-28-2010
anurag;

your result gave the whole down.txt Smilie

---------- Post updated at 17:54 ---------- Previous update was at 17:51 ----------

oh; no no no; it is ok now Smilie i have just replaced the filenames. it is ok now Smilie thx very muc
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to align/sort the column pairs of an csv file, based on keyword word specified in another file?

I have a csv file as shown below, xop_thy 80 avr_njk 50 str_nyu 60 avr_irt 70 str_nhj 60 avr_ngt 50 str_tgt 80 xop_nmg 50 xop_nth 40 cyv_gty 40 cop_thl 40 vir_tyk 80 vir_plo 20 vir_thk 40 ijk_yuc 70 cop_thy 70 ijk_yuc 80 irt_hgt 80 I need to align/sort the csv file based... (7 Replies)
Discussion started by: dineshkumarsrk
7 Replies

2. UNIX for Dummies Questions & Answers

UNIX Scripting help to input string and search a file to find

Hi everyone, I am new to Unix and need help writing a script that can ask user for an input, then search that input within a file I know will have to use the read and grep commands, anyone can give me somewhere to start would help Task: Write a script to display... (1 Reply)
Discussion started by: 12ic11
1 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. Solaris

Keyword search input from a file

Hi, I have a file which got only one column and got some keywords. I have another file where the keywords used in the first file are repeated in the second file. Now I would like to know how many times each keyword from the first file is repeated in the second file. Request your help on... (1 Reply)
Discussion started by: pointers
1 Replies

5. Shell Programming and Scripting

How to search for a file without using find or which?

Hello, I am a newbie to Unix shell script writing. I have to write a Korn or Bash shell script to search for a file, where some file named <filename> is given at the prompt. So let's say the script command will be called locate_file. Then one could enter locate_file filename. The... (1 Reply)
Discussion started by: TPaine_4liberty
1 Replies

6. UNIX for Dummies Questions & Answers

how to find a word in a file that appears next to a given keyword

Hi Experts, I have a file which contains some text. i need to print the word next to a given keyword. Please help. Ex: test.txt ===================== NEXT HOST ===================== AEADBAS001 access-list 1 permit xxxxxxxxxxxxxx ip access-list extended BLA_Outgoing_Filter... (6 Replies)
Discussion started by: mwrg
6 Replies

7. Shell Programming and Scripting

Search and find the string and output to a new file.

Hi I want to search the following in the entire file and put them in a diff file. file 1 contains : Non error probabilities are not decreasing in curve 1GEORGE_SGD_SUB Mid Point RESCAP_SGD_SNU curve have errors Non default probabilities are not decreasing in curve ABF_JPY_SUB Mid Point... (3 Replies)
Discussion started by: szchmaltz
3 Replies

8. Shell Programming and Scripting

how to search a keyword within a file using a for loop

hi guys i have a problem here, im trying to stablish a relationship between a text file and an input user for example the script is going to prompt the user for some football team and what the script is going to do is return the colums in which that input is located so far this is what i have ... (6 Replies)
Discussion started by: lucho_1
6 Replies

9. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies

10. UNIX for Advanced & Expert Users

find file with date and recursive search for a text

Hey Guyz I have a requirement something like this.. a part of file name, date of modification of that file and a text is entered as input. like Date : 080206 (MMDDYY format.) filename : hotel_rates text : Jim now the file hotel_rates.ZZZ.123 (creation date is Aug 02 2006) should be... (10 Replies)
Discussion started by: rosh0623
10 Replies
Login or Register to Ask a Question