![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| file Lookup using awk | jerome Sukumar | Shell Programming and Scripting | 1 | 08-30-2007 12:28 AM |
| Lookup with a file | pavan_test | UNIX for Dummies Questions & Answers | 5 | 07-21-2006 07:57 AM |
| reverse lookup file problem | Westy564 | IP Networking | 2 | 01-09-2004 10:55 AM |
| file lookup | gillbates | UNIX for Dummies Questions & Answers | 6 | 12-12-2003 10:04 AM |
| grep multiple text files in folder into 1 text file? | coppertone | UNIX for Dummies Questions & Answers | 7 | 08-23-2002 11:50 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
getting particular text after grep from lookup file
Hi
I have two files a1 and b1 a1 has the following job names ab cd ef b1 has the following job details /*----------- ji -----------------*/ asdasd fgd saas dfdf asas fd gfg /*---------- ab ----------------*/ ara jhk dfhk asjla condition: s(abc_wf_hi) /*---------- jkl ----------------*/ hdkash da asjdhka aksja ab /*-------- cd ----------------*/ ujhy juik condition: s(lkj_ins_ko) /*-------- ef ---------------*/ hjk kjsdh sdjsl kjlsd hjk hkss sdsd condition: s(jkllk_wf_kol) The names within the /*---- and -----*/ represent job names and the text that follows represents the details of the jobs. I need to search for each job of file a1 in file b1 and if a match is found, then I need to write all the details of the job in a file. For Eg: If the job being searched is cd,then it should write the following to a file: ujhy juik condition: s(lkj_ins_ko) Searching for the pattern is easy but after that I get stuck. I tried removing the '*' from the front and '*/' from the last on the job name lines in file b1.Then I used '/' as the field delimiter with awk but to no avail. below is the script in bash shell: cat a1|while read line do sed -e 's:\*::' -e 's:\*\/::' b1|awk -F"/" '/$line/'>o_file done I also tried removing the newlines so the whole of file b becomes a single line with fields separated by '/' but even that doesnt work. sed 's/\n//' b1 --this doesnt work Plz help. I am new to DB administration and if i dont get the answer to this, I'll have to do the whole thing manually. The file b1 in reality contains some 800 jobs. Imagine!! Last edited by napolayan; 10-18-2006 at 06:43 AM. Reason: additional information |
| Forum Sponsor | ||
|
|
|
|||
|
Python alternative:
Code:
a1_data = open("file1.txt").readlines() #['ab\n','cd\n','ef\n']
a1_data = [i.strip() for i in a1_data] #['ab','cd','ef']
a2_data = open("file2.txt").readlines() #read all of file into list
flag = False
for job in a1_data: #iterate thru ab,cd,ef
for lines in a2_data:
if lines.startswith("/") and job in lines:
flag = True
if lines.startswith("/") and not job in lines:
flag = False
if flag:
print lines.strip()
Code:
/home> python test.py /*---------- ab ----------------*/ ara jhk dfhk asjla condition: s(abc_wf_hi) /*-------- cd ----------------*/ ujhy juik condition: s(lkj_ins_ko) /*-------- ef ---------------*/ hjk kjsdh sdjsl kjlsd hjk hkss sdsd condition: s(jkllk_wf_kol) |
|
|||
|
thanks anbu123
ur code is working fine, but i want to clarify my understanding of it. /\/\*--* $str --*\*\// this portion i got. u are searching for the string, fine. ,/^ *$/ p two questions here..pardon my ignorance of this format (i am a newbie after all n what abt the mystery of there apparently being no newline character after each line of b1? P.S. thanks to u too ghostdog74, but i am an ignoramus when it comes to python so everything went just like electricity does - Overhead Transmission. |
|
|||
|
Quote:
|
|
|||
|
If you don't need to output the first and last (empty) line:
Code:
sed -n "/-- ab --/, /^$/ {/-- ab --/b;/^$/b;p}" b1
Code:
awk 'BEGIN{FS="\n"; RS="\n\n"; OFS="\n"} /-- ab --/ {for (i = 2; i <= NF; i++) print $i}' b1
|
|||
| Google The UNIX and Linux Forums |