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
# 8  
Old 10-19-2006
Hi ripat

u almost read my mind. removing those things will be helpful but not necessary for me. My final objective, which i didnt mention, was to search for pattern 'condition' after retrieving the text and then output the job name which is there after 'condition'.

I tried running the code u gave and this is what its giving

while read str
> do
> sed -n "/-- $str --/, /^$/ {/-- $str --/b;/^$/b;p}" b1
> done<a1
sed: command garbled: /-- ab --/, /^$/ {/-- ab --/b;/^$/b;p}
sed: command garbled: /-- cd --/, /^$/ {/-- cd --/b;/^$/b;p}
sed: command garbled: /-- ef --/, /^$/ {/-- ef --/b;/^$/b;p}

can u explain the command line in a bit of detail...for e.g. what is the b obtion for :- {/-- $str --/b;/^$/b;p} is it for replacing with a blank?

I also ran the awk script. That is not returning anything..just the command prompt.

while read str
do
awk 'BEGIN{FS="\n"; RS="\n\n"; OFS="\n"} /-- ab --/ {for (i = 2; i <= NF; i++) print $i}' b1
done<a1
# 9  
Old 10-19-2006
Quote:
Originally Posted by napolayan
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
"/\/\*--* $str --*\*\//,/^ *$/ p
Here trying to match from a line with pattern /\/\*--* $str --*\*\// to another line with the pattern /^ *$/.If you want to specify two address then you have to separate it with comma.

/^ *$/
* matches any number or none of the single character that immediately precedes it.So this will match empty line as well as lines with blanks.
# 10  
Old 10-20-2006
Quote:
Originally Posted by anbu23
"/\/\*--* $str --*\*\//,/^ *$/ p
Here trying to match from a line with pattern /\/\*--* $str --*\*\// to another line with the pattern /^ *$/.If you want to specify two address then you have to separate it with comma.

/^ *$/
* matches any number or none of the single character that immediately precedes it.So this will match empty line as well as lines with blanks.
thanks anbu

this funda i hadnt come across in the 2 months that i hv been learning unix. i thought only sed '1,3 p' works. didnt know patterns can also be matched the same way.
# 11  
Old 10-20-2006
Quote:
Originally Posted by napolayan
Hi ripat

u almost read my mind. removing those things will be helpful but not necessary for me. My final objective, which i didnt mention, was to search for pattern 'condition' after retrieving the text and then output the job name which is there after 'condition'.

I tried running the code u gave and this is what its giving

while read str
> do
> sed -n "/-- $str --/, /^$/ {/-- $str --/b;/^$/b;p}" b1
> done<a1
sed: command garbled: /-- ab --/, /^$/ {/-- ab --/b;/^$/b;p}
sed: command garbled: /-- cd --/, /^$/ {/-- cd --/b;/^$/b;p}
sed: command garbled: /-- ef --/, /^$/ {/-- ef --/b;/^$/b;p}

can u explain the command line in a bit of detail...for e.g. what is the b obtion for :- {/-- $str --/b;/^$/b;p} is it for replacing with a blank?

I also ran the awk script. That is not returning anything..just the command prompt.

while read str
do
awk 'BEGIN{FS="\n"; RS="\n\n"; OFS="\n"} /-- ab --/ {for (i = 2; i <= NF; i++) print $i}' b1
done<a1
b is for branching. If label is given control will transfer to that label or else control is transferred to the end of the script.
You can use d to achieve the same result

Code:
while read str
do
      sed  -n "/-- $str --/, /^$/ {/-- $str --/d;/^$/d;p}" b1
done<a1

 
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