grep for words in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep for words in file
# 1  
Old 12-29-2011
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
# 2  
Old 12-29-2011
So, if one line contains both 'word1' and 'word2' and you grep for both the words, you want the same line printed twice each on a new line?
# 3  
Old 12-29-2011
grep for words in file

Hi

No, if two words are on the same line, they can be shown on the same line, but grep finds one word on a line, it should show on a new line.
The ideia is after find the words, use mailx to mail the lines found, but on my outlook I am getting everything in one line....

regards

FR
# 4  
Old 12-29-2011
Code:
$ grep -f pattern_filename filename

where pattern_filename look like:
Code:
pattern1
pattern2
....
....


Last edited by Franklin52; 12-29-2011 at 10:45 AM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 12-29-2011
Quote:
Originally Posted by fretagi
Hi

No, if two words are on the same line, they can be shown on the same line, but grep finds one word on a line, it should show on a new line.
The ideia is after find the words, use mailx to mail the lines found, but on my outlook I am getting everything in one line....

regards

FR
If your input files is having a valid and contains newline character after each line, you getting probably getting the new line at destination.
It might be the client ( outlook ) or the encoding system which doesn't display it properly.


About your problem, how do you want to grep for the patterns (is it OR or AND between the patterns)?

Also, tell us the OS version on which you are working. and post the sample data and required output.
# 6  
Old 12-29-2011
Code:
$ grep -f pattern_filename filename
where pattern_filename look like:
pattern1
pattern2
....
....
patternn
This User Gave Thanks to tarun_agrawal For This Post:
# 7  
Old 12-29-2011
Hi

Tarun Agrawal

Your suggestion is working fine, but what do I change to show me the last 20 lines only.

Anchal Khare:

The O.S. is hp-ux, the file is /var/adm/syslog/syslog.log

regards

FR
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

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

3. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 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

Splitting concatenated words in input file with words from the same file

Dear all, I am working with names and I have a large file of names in which some words are written together (upto 4 or 5) and their corresponding single forms are also present in the word-list. An example would make this clear annamarie mariechristine johnsmith johnjoseph smith john smith... (8 Replies)
Discussion started by: gimley
8 Replies

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

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

8. Shell Programming and Scripting

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> ... (7 Replies)
Discussion started by: thepurple
7 Replies

9. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

10. 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
Login or Register to Ask a Question