getting particular text after grep from lookup file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers getting particular text after grep from lookup file
# 1  
Old 10-18-2006
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 10:43 AM.. Reason: additional information
# 2  
Old 10-18-2006
try this

Code:
while read str
do 
    sed -n "/\/\*--* $str --*\*\//,/^ *$/ p" b1
done<a1

# 3  
Old 10-18-2006
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  
Old 10-19-2006
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 Smilie ), 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. Smilie
# 5  
Old 10-19-2006
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. Smilie
No worries. Smilie 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 Smilie
# 6  
Old 10-19-2006
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  
Old 10-19-2006
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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to replace text file from a lookup file

Hi. I need assistance with the replacing of text into a specific file via a bash script. My bash script, once run, currently provides a menu of computer names to choose.The script copies onto my system various files, depending what computer was selected in the menu.This is working OK. Now, I... (1 Reply)
Discussion started by: jonesn2000
1 Replies

2. Shell Programming and Scripting

Hostname lookup and create text file

I have a list of hostnames in a txt file . I need to do nslookup or other command on linux and get the ip address and if you dont find an ip address then put 0.0.0.0 instead in the output text file along with the hostname. So input host1 host2 host2.dd.ddd.net Output host1,... (2 Replies)
Discussion started by: gubbu
2 Replies

3. Linux

Grep the ip to a text file

Hi all, We Have Squid server, We need to get the particular IP's log from /var/log/squid/access.log, if i need to get only the log's of 192.168.0.99, How can i get the log's to a separate file. Here is the sample log what i have got from access.log file 1392706763.690 847... (2 Replies)
Discussion started by: babinlonston
2 Replies

4. Shell Programming and Scripting

How to grep a log file for words listed in separate text file?

Hello, I want to grep a log ("server.log") for words in a separate file ("white-list.txt") and generate a separate log file containing each line that uses a word from the "white-list.txt" file. Putting that in bullet points: Search through "server.log" for lines that contain any word... (15 Replies)
Discussion started by: nbsparks
15 Replies

5. Shell Programming and Scripting

Script help needed(lookup 2 text files)

Hi, iam new to unix scripting need a help. The scenario is i have 2 text files. a.txt has colA colB colC colD JKU1270A JKU1042A 9 1 JKU1270A JKU1042B 9 1 JKU1270A JKU1042C 9 1 JKU1270B JKU1107B 9 1 JKU1270B JKU1107C 9 1... (1 Reply)
Discussion started by: tech_frk
1 Replies

6. UNIX for Dummies Questions & Answers

How to grep multiple lines from a text file using another text file?

I would like to use grep to select multiple lines from a text file using a single-column text file. Basically I want to only select lines from the first text file where the second column of the first text file matches the second text file. How do I go about doing that? Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

7. Shell Programming and Scripting

search text file in file if this file contains necessary text (awk,grep)

Hello friends! Help me pls to write correct awk and grep statements for my task: I have got files with name filename.txt It has such structure: Start of file FROM: address@domen.com (12...890) abc DATE: 11/23/2009 on Std SUBJECT: any subject End of file So, I must check, if this file... (4 Replies)
Discussion started by: candyme
4 Replies

8. Shell Programming and Scripting

Text file with grep

Sirs, i am trying to create simple script file.. what i do is grep for a pattern and output 1 line after it. exmp: grep -A 1 time files.txt Output: time file1 time file2 --- Is there some option I can use so I can get on result each 2 lines combined as one file? like: time file1... (2 Replies)
Discussion started by: alekkz
2 Replies

9. UNIX for Advanced & Expert Users

Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help

Write a quick shell snippet to find all of the IPV4 IP addresses in any and all of the files under /var/lib/output/*, ignoring whatever else may be in those files. Perform a reverse lookup on each, and format the output neatly, like "IP=192.168.0.1, ... (0 Replies)
Discussion started by: choco4202002
0 Replies

10. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies
Login or Register to Ask a Question