The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 03:28 AM
Lookup with a file pavan_test UNIX for Dummies Questions & Answers 5 07-21-2006 10:57 AM
reverse lookup file problem Westy564 IP Networking 2 01-09-2004 02:55 PM
file lookup gillbates UNIX for Dummies Questions & Answers 6 12-12-2003 02:04 PM
grep multiple text files in folder into 1 text file? coppertone UNIX for Dummies Questions & Answers 7 08-23-2002 02:50 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-18-2006
napolayan napolayan is offline
Registered User
  
 

Join Date: Oct 2006
Location: Bangalore, India
Posts: 41
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 09:43 AM.. Reason: additional information
  #2 (permalink)  
Old 10-18-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
try this

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

Join Date: Sep 2006
Posts: 2,507
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)
  #4 (permalink)  
Old 10-19-2006
napolayan napolayan is offline
Registered User
  
 

Join Date: Oct 2006
Location: Bangalore, India
Posts: 41
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.
  #5 (permalink)  
Old 10-19-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
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
  #6 (permalink)  
Old 10-19-2006
napolayan napolayan is offline
Registered User
  
 

Join Date: Oct 2006
Location: Bangalore, India
Posts: 41
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?
  #7 (permalink)  
Old 10-19-2006
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0