The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-18-2006
Registered User
 

Join Date: Oct 2006
Location: Bangalore, India
Posts: 41
Stumble this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-18-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Stumble this Post!
try this

Code:
while read str
do 
    sed -n "/\/\*--* $str --*\*\//,/^ *$/ p" b1
done<a1
Reply With Quote
  #3 (permalink)  
Old 10-18-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,542
Stumble this Post!
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()
output:
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)
Reply With Quote
  #4 (permalink)  
Old 10-18-2006
Registered User
 

Join Date: Oct 2006
Location: Bangalore, India
Posts: 41
Stumble this Post!
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 ), but why the comma? n the ^ *$ will return everything till it encounters a space at the end, right?

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.
Reply With Quote
  #5 (permalink)  
Old 10-18-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,542
Stumble this Post!
Quote:
Originally Posted by napolayan
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.
No worries. Its just an alternative, without the need for regular expression, and the "algorithm" can be implemented in your preferred choice of language too. Just like your electricity switch, you just turn it on and off
Reply With Quote
  #6 (permalink)  
Old 10-19-2006
Registered User
 

Join Date: Oct 2006
Location: Bangalore, India
Posts: 41
Stumble this Post!
hi anbu123

got some of it /^ *$/ matches an empty line, right? but that can be done with /^$/ also, can't it? and is the comma specifying the range? i.e. from the pattern till an empty line is found?
Reply With Quote
  #7 (permalink)  
Old 10-19-2006
Registered User
 

Join Date: Oct 2006
Location: Belgium
Posts: 171
Stumble this Post!
If you don't need to output the first and last (empty) line:

Code:
sed  -n "/-- ab --/, /^$/ {/-- ab --/b;/^$/b;p}" b1
Or, with awk (even though I feel it could be a lot shorter):

Code:
awk 'BEGIN{FS="\n"; RS="\n\n"; OFS="\n"} /-- ab --/ {for (i = 2; i <= NF; i++) print $i}' b1
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0