recursively Grep particular words among lines from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting recursively Grep particular words among lines from a file
# 1  
Old 06-22-2011
recursively Grep particular words among lines from a file

dear experts,

Could you please help me to write a command/script to find follwing:

from the log file i want to grep "resCode89270200100001552311" after that only "<resultCode>40614</resultCode>"

it will be like that:
resCode89270200100001552311
<resultCode>40614</resultCode>


2011-06-21 13:42:23.582 | DEBUG | OSS | 1308656508323628915 | resCode89270200100001552311 | com..oss.inventory.operate
2011-06-21 13:42:23.582 | DEBUG | OSS | 1308656508323628915 | Begin queryResource | com.oss.inventory.operate.dbm.InvOperQueryDBM.queryResource(InvOperQueryDBM.java:1352)
...
...there are many lines
..
..
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:196)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:751)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:881)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1473)
<ResultOfOperationReply>
<resultCode>40614</resultCode>
</ResultOfOperationReply>
# 2  
Old 06-22-2011
what about egrep "pattern1|pattern2" ?.

Please paste a sample input and expected out (I'm not sure with your current sample data,sorry.)
# 3  
Old 06-22-2011
Quote:
Originally Posted by panyam
what about egrep "pattern1|pattern2" ?.

Please paste a sample input and expected out (I'm not sure with your current sample data,sorry.)
egrep "pattern1|patter2" is not worked. And the pattern is just like above. Consider those lines are in a file. I will try to grep the patters. And output will be as below.
resCode89270200100001552311
<resultCode>40614</resultCode>
# 4  
Old 06-22-2011
With the limited knowledge of your input and expected output, the below works:

Code:
 
awk  -v s="resCode89270200100001552311" '$0 ~ s {c=1;} c>0&&/resultCode/ { print s"\n"$0; exit }' input_file

# 5  
Old 06-22-2011
@thepurple
Please repost a better example , your first post is confusing, thanks to clarify your needs.
# 6  
Old 06-22-2011
Hi thepurple,

I dont know whether my thought is correct or not. I am very new to the script and trying to get the solution. Please check the below codes, I may not sure that this will help you..

Your file
Code:
$ cat temp1
2011-06-21 13:42:23.582 | DEBUG | OSS | 1308656508323628915 | resCode89270200100001552311 | com..oss.inventory.operate
2011-06-21 13:42:23.582 | DEBUG | OSS | 1308656508323628915 | Begin queryResource | com.oss.inventory.operate.dbm.InvOperQueryDBM.queryResource(InvOperQueryDBM.java:1352)
...
...there are many lines
..
..
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:196)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:751)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:881)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1473)
<ResultOfOperationReply>
<resultCode>40614</resultCode>
</ResultOfOperationReply>

Commands copied into a file "checking"

Code:
$ cat checking
cat temp1 | head -1 | awk '{ print $10 }'
cat temp1 | tail -2 | head -1

Exectuted the file..

Code:
$ ./checking
resCode89270200100001552311
<resultCode>40614</resultCode>


Last edited by pludi; 06-22-2011 at 08:04 AM..
# 7  
Old 06-22-2011
Don't know if it fits your need but you can give a try to :
Code:
awk -F\| '/resCode/&&NF>4{print $5;next}/resultCode/&&NF==1' input

If running SunOS/solaris, use nawk or /usr/xpg4/bin/awk instead of awk
This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple words in a file with help of fixed string switch

I have multiple strings in a file which have special character $, when i search strings by ignoring $ with \ using single quotes it returns empty results. My search strings are set char_1($lock) and set new_char_clear_3($unlock) I tried searching with but it returns empty results.However... (3 Replies)
Discussion started by: g_eashwar
3 Replies

2. Shell Programming and Scripting

Filter all the lines with minimum specified length of words of a text file

Hi Can someone tell me which script will work best (in terms of speed and simplicity to write and run) for a large text file to filter all the lines with a minimum specified length of words ? A sample script with be definitely of great help !!! Thanks in advance. :) (4 Replies)
Discussion started by: my_Perl
4 Replies

3. 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

4. Shell Programming and Scripting

grep for words in file

Hi Please can you help me on this: How to grep for multiple words in a file, BUT for every word found output it to a new line. regards FR (8 Replies)
Discussion started by: fretagi
8 Replies

5. Shell Programming and Scripting

Grep multiple words in a single file

Hello All, I'm a newbie/rookie in Shell scipting. I've done oracle export of a table using Export utility. When I do export, it generates 2 files. 1> .dmp file 2> .dmp.log file. In .dmp.log file I have to search for a sentence which goes like '0 records have been inserted' and then... (2 Replies)
Discussion started by: samfisher
2 Replies

6. Shell Programming and Scripting

grep words from output file

Hi, By using shell scripit i have save output in one file. I want to grep two words named CLUSTER and CLUSQMGR from that output file. How to grep that. output file would be having below words TYPE(QCLUSTER) ALTDATE(2010-05-17) CLUSTER(QS.CL.MFT1) ... (5 Replies)
Discussion started by: darling
5 Replies

7. Shell Programming and Scripting

Need to parse lines in a file into two words and assign the values to two variables

For example, I have a file with below lines containing VOB tags and VOB paths. * /vobs/fts/FTSUSM20_VOB /ccvobsslx01/projects/vobs/eml/FTSUSM20_VOB * /vobs/fts/FTS20_VOB /ccvobsslx01/projects/vobs/eml/FTS20_VOB * /vobs/pmv/PMS_VOB /ccvobsslx01/projects/vobs/cpm/_/PMS_VOB *... (4 Replies)
Discussion started by: senthilkc
4 Replies

8. Shell Programming and Scripting

how to remove words between /* and */ in several lines of file

hi, I want to remove comments in somany files. coments started from /* and ends with */. so i wnt to remove words /* to */ how to remove words between /* and */ in several lines of file EX : cat haha.txt "HI",/*hk*/ob1,raju,/*hjh*/boju,beeju output should be : "HI",ob1,raju,boju,beeju ... (8 Replies)
Discussion started by: spc432
8 Replies

9. Shell Programming and Scripting

How to from grep command from a file which contains matching words?

Hi all I have a file with below content (content is variable whenever new product is launched). I need form a grep command like this egrep "Unknown product|Invalid symboland so on" How to do it using a script? Unknown product Invalid symbol No ILX exch found exceeds maximum size AFX... (4 Replies)
Discussion started by: johnl
4 Replies

10. Shell Programming and Scripting

Ignore some lines with specific words from file comparison

Hi all, I need help in doing this scenario. I have two files with multiple lines. I want to compare these two files but ignoring the lines which have words like Tran, Loc, Addr, Charge. Also if i have a word Credit in line, i want to tokenize (i.e string after character " ... (2 Replies)
Discussion started by: jakSun8
2 Replies
Login or Register to Ask a Question