How to grep keywords?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep keywords?
# 1  
Old 04-18-2014
How to grep keywords?

I have below text file only with one line:

Code:
vi test.txt

This is the first test from a1.loa1 a1v1, b2.lob2, "c3.loc3" c3b1, loc4 but not from mot3 and second test from a5.loa5


Below should be the output that i want:
Code:
a1.loa1
b2.lob2
c3.loc3
loc4
a5.loa5

alv1 and c3b1 should be ignore.


The rules would be like below:
1) anything before keyword "from" should be ignore
2) anything after keyword "but" should be ignore if any

so we narrow down to below seach:
"from a1.loa1 a1v1, b2.lob2, "c3.loc3" c3b1, loc4"
and
"from a5.loa5"
1) second sentence after keyword "from" should be ignore if any (example: a1v1 should be ignore)
2) first sentence after the comma would needed (example b2.lob2, c3.loc3 and loc4 are needed)
3) second sentence after the comma should be ignore (example c3b1)
4) any double quotes should be remove (example "c3.loc3" should be output as c3.loc3 - no double quotes)

should have the output as below:
Code:
a1.loa1
b2.lob2
c3.loc3
loc4
a5.loa5

I know that this is a bit tricky, but this is required from my application, I would indeed really appreciate if someone could help me on above.

Thanks

Last edited by Don Cragun; 04-18-2014 at 05:44 AM.. Reason: Add CODE tags.
# 2  
Old 04-18-2014
Hello,

Please use the code tags as per forum rules, also please let us know the input for same.


Thanks,
R. Singh
# 3  
Old 04-18-2014
Hi,

This is my text file:

Code:
This is the first test from a1.loa1 a1v1, b2.lob2, "c3.loc3" c3b1, loc4 but not from mot3 and second test from a5.loa5

The output should look at below:
Code:
a1.loa1
b2.lob2
c3.loc3
loc4
a5.loa5

I don't have any input yet, as i not sure how to start, perhaps it should be using awk or sed and I would be thankful for anyone can help me with this.

Thanks
Moderator's Comments:
Mod Comment Please use CODE (not ICODE) tags for multi-line input, output, and code samples.

Last edited by Don Cragun; 04-18-2014 at 05:42 AM.. Reason: Fix CODE tags.
# 4  
Old 04-18-2014
Code:
awk '{split($0, a, "from");
  for(i = 2; i <= length(a); i += 2)
    {split(a[i], b, "but");
    split(b[1], c, ", ");
    for(j = 1; j <= length(c); j++)
      {gsub("\"", X, c[j]);
      split(c[j], d, " ");
      print d[1]}}}' test.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep multiple keywords from a file

I have a script that will search for a keyword in all the log files. It work just fine. LOG_FILES={ "/Sandbox/logs/*" } for file in ${LOG_FILES}; do grep $1 $file done This only works for 1 keyword. What if I want to search for more then 1 keywords, say 4 or maybe even... (10 Replies)
Discussion started by: Loc
10 Replies

2. AIX

Filtering keywords from syslog.

Hi, My syslog in AIX forwards all user facility to a specific log /logs/user.log I need to further segregate the user.log to logs specific to various applications and i was wondering if i can make some configuration change to syslog.conf to forward messages based on a certain keyword? for... (2 Replies)
Discussion started by: roshan.171188
2 Replies

3. Shell Programming and Scripting

Grep Keywords one by one

Hi I am trying to determine number of lines having a specific keyword. So for that I am using below query: grep -i 'keyword1' filename|wc -l This give me number of lines. Perfect for me. However now the requirement is I have multiple keywords together... and I have to find number of... (3 Replies)
Discussion started by: dashing201
3 Replies

4. Shell Programming and Scripting

Extract word between two KEYWORDS

Hi I want to extract all the words between two keywords HELLO & BYE. eg: Input 1_HELLO_HOW_ARE_YOU_BYE_TEST 1_HELLO_WHERE_ARE_BYE_TEST 1_HELLO_HOW_BYE_TEST Output Required: HOW_ARE_YOU WHERE_ARE HOW (7 Replies)
Discussion started by: dashing201
7 Replies

5. Shell Programming and Scripting

Parsing with keywords

Hi All, Please help with code for this. I want to parse several huge files and summarize relevant information into columns. The columns of output are title, pagebegin,pageend, author1,author2....,author8, abstract. Column descriptions are as follows. Title Line after single integer value... (3 Replies)
Discussion started by: alpesh
3 Replies

6. UNIX for Dummies Questions & Answers

finding keywords in many files using grep

Hi to all Sorry for the confusion because I did not explain the task clearly. There are many .hhr files in a folder There are so many lines in these .hhr files but I want only the following 2 lines to be transferred to the output file. The keyword No 1 and all the words in the next line They... (5 Replies)
Discussion started by: raghulrajan
5 Replies

7. Shell Programming and Scripting

searching keywords in file

hey guys, Hey all, I'm doing a project currently and want to index words in a webpage. So there would be a file with webpage content and a file with list of words, I want an output file with true and false that would show which word exists in the webpage. example: Webpage content... (2 Replies)
Discussion started by: Johanni
2 Replies

8. Shell Programming and Scripting

Search a file with keywords

Hi All I have a file of format asdf asf first sec endi asdk rt 123 ferf dfg ijglkp (7 Replies)
Discussion started by: mailabdulbari
7 Replies

9. Shell Programming and Scripting

How to cut id between keywords?

Hi, how to cut id from line ? ....<a class='adata' href='User.php?uid=545554'>.... to 545554 (3 Replies)
Discussion started by: Trump
3 Replies

10. Shell Programming and Scripting

Regarding use and require keywords

Hi, what is the difference between use and require keywords in Perl. What is the significance of these lines (what it mean, what is the use of this) #!/usr/bin/perl -w // In Perl script.... #!/bin/ksh //In shell script..... Thanks Sweta (2 Replies)
Discussion started by: sweta
2 Replies
Login or Register to Ask a Question