finding keywords in many files using grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers finding keywords in many files using grep
# 1  
Old 03-13-2012
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 are
No 1
>UP20 ..................................

The word No 1 occurs only once in a file
I have some 4000 files and I want only these 2 lines from each file.

I hope the problem is explained ok.

thank you
raghul

Last edited by raghulrajan; 03-14-2012 at 08:31 AM.. Reason: for better understanding
# 2  
Old 03-13-2012
BLANKED POST.
O/P has completely re-worded post #1 and it now makes no sense.

Last edited by methyl; 03-14-2012 at 12:25 PM.. Reason: post #1 has completely changed
# 3  
Old 03-13-2012
Does the following meet your needs?
PHP Code:
egrep -"Line 1|info1" . >output.txt 
or
PHP Code:
grep -"Line 1|info1" * > output.txt 
or
PHP Code:
cat * |grep -"Line 1|info1" output.txt 
# 4  
Old 03-14-2012
Hi
I never knew I could work like with an OS. I have recently migrated to linux.
I think they should start teaching Linux/unix seriously from middle school level.
Thanks for the reply.
I did not get the desired result
I will give you what I did below, I am not sure whether i did properly.
When i execute the command it shows >, I dont know the meaning of this

Code:
raghul@raghul-Studio-1749:~/TOOLS/hhblits/db/blits1$ head -2 *.hhr 
==> CONTIG07340_NA______CONTIG22498_1_1_53_645_196_.hhr <== 
Query         CONTIG07340|NA______CONTIG22498_1_1_53_645_196_ 
Match_columns 152 
 
==> ISOTIG00171_NA______CONTIG00242_1_4_182_1892_502_PLUS.hhr <== 
Query         ISOTIG00171|NA______CONTIG00242_1_4_182_1892_502_PLUS 
Match_columns 210 
 
==> ISOTIG00273_NA______CONTIG60455_1_1_66_494_142_.hhr <== 
Query         ISOTIG00273|NA______CONTIG60455_1_1_66_494_142_ 
Match_columns 84 
 
raghul@raghul-Studio-1749:~/TOOLS/hhblits/db/blits1$ ls -1d *\.hhr 2>/dev/null | while read *.hhr do head -2 "${*.hhr}" done >output.txt 
>

---------- Post updated at 03:36 AM ---------- Previous update was at 03:27 AM ----------

Hi @dagio
Thanks for the reply. But the output files are empty.
I have posted a message, I think that explains the nature of the task
raghul

Last edited by Corona688; 03-14-2012 at 12:10 PM.. Reason: Code tags for code, please.
# 5  
Old 03-14-2012
Code:
 
for i in *.hhr; do echo "==> $i <=="; head -2 $i; done > output.txt

# 6  
Old 03-14-2012
Hi,
You need to replace the strings according to your files.
This will work only if always the first line contains ==> , second line contains the word Query and also those two words does not appeared in any other line.

example:
Code:
cat * |grep -E "==>|Query"

output:

Code:
==> CONTIG07340_NA______CONTIG22498_1_1_53_645_196_.hhr <== 
Query CONTIG07340|NA______CONTIG22498_1_1_53_645_196_ 
 ==> ISOTIG00171_NA______CONTIG00242_1_4_182_1892_502_PLUS.hhr <== 
Query ISOTIG00171|NA______CONTIG00242_1_4_182_1892_502_PLUS

 
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. UNIX for Dummies Questions & Answers

Find keywords in multiple log files

The Problem that I am having is when the code ran and populated the progflag.csv file, columns MEMSIZE, SECOND and SASEXE were blank. The next problems are the IF else statement isn't working and the email function isn't sending the progflag.csv attachment. a. What I want the program to do is to... (2 Replies)
Discussion started by: dellanicholson
2 Replies

3. Shell Programming and Scripting

Search files in directory for keywords using bash

I have ~100 text files in a directory that I am trying to parse and output to a new file. I am looking for the words chr,start,stop,ref,alt in each of the files. Those fields should appear somewhere in those files. The first two fields of each new set of rows is also printed. Since this is on a... (7 Replies)
Discussion started by: cmccabe
7 Replies

4. Shell Programming and Scripting

How to grep keywords?

I have below text file only with one line: 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: a1.loa1 b2.lob2 c3.loc3 loc4 a5.loa5 alv1 and c3b1 should be... (3 Replies)
Discussion started by: khchong
3 Replies

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

6. UNIX for Advanced & Expert Users

Need to search for keywords within files modified at a certain time

I have a huge list of files in an Unix directory (around 10000 files). I need to be able to search for a certain keyword only within files that are modified between certain date and time, say for e.g 2012-08-20 12:30 to 2012-08-20 12:40 Can someone let me know what would be the fastest way... (10 Replies)
Discussion started by: virtual123
10 Replies

7. UNIX for Advanced & Expert Users

Finding/Grep on files with date and hour in the file name

Hi, I have a folder structure as follows, DATA -> 2012-01-01 -> 00 -> ABC_2012-01-03_00.txt -> 01 -> ABC_2012-01-03_01.txt -> 02 -> ABC_2012-01-03_02.txt ... -> 23 -> ABC_2012-01-03_02.txt -> 2012-01-02 ... (1 Reply)
Discussion started by: mihirvora16
1 Replies

8. Shell Programming and Scripting

Finding/Grep on files with date and hour in the file name

Hi, I have a folder structure as follows, DATA -> 2012-01-01 -> 00 -> ABC_2012-01-03_00.txt -> 01 -> ABC_2012-01-03_01.txt -> 02 -> ABC_2012-01-03_02.txt ... -> 23 -> ABC_2012-01-03_02.txt -> 2012-01-02 -> 2012-01-03 So the dir DATA contains the above hierarchy, User input Start and... (6 Replies)
Discussion started by: mihirvora16
6 Replies

9. Shell Programming and Scripting

Finding 50k Keywords in 3k files

Hi, I have a file with about 50k keywords. I have a requirement to scan about 3k files to identify which filename has which keyword i.e. an output like following: File1,Keyword1 File1,Keyword2 File3,Keyword1 ..... I have written a shell script which takes each of the 3k files, searches... (4 Replies)
Discussion started by: rjains
4 Replies

10. Shell Programming and Scripting

Finding the executable files of a directory using Grep

Hi guys, Can you please help me print all the executable files of a directory(in this case /home) using grep? All i know is that this command should do it but it doesnt... ls -l ~ | grep -..x it shows me the following mesage grep: invalid option -- '.' Χρήση: grep ... ΥΠΟΔΕΙΓΜΑ ... (3 Replies)
Discussion started by: jimas13
3 Replies
Login or Register to Ask a Question