Extract Lines Containg a Keyword


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract Lines Containg a Keyword
# 1  
Old 10-03-2011
Extract Lines Containg a Keyword

Hi ,

I have two files, say KEY_FILE and the MAIN_FILE. I am trying to read the KEY_FILE which has only one column and look for this column data in the MAIN_FILE to extract all the rows that have this key.

I have written a script to do so, but somehow it is not returning all the rows (
It gives the data for last row in KEY_FILE only.
). I feel there is something wrong with the loop. Here is the script

Quote:
#!/bin/ksh
KEY_FILE=KeyFile.txt
MAIN_FILE=MainFile.txt
OUTPUT_FILE=Output.txt
while read inputline
do
column=`echo "$inputline" | awk '{print $0}'`
grep "$column" $MAIN_FILE > $OUTPUT_FILE
done < $KEY_FILE
Please see what i am missing here & do advise a better way if possible. Thanks.
# 2  
Old 10-03-2011
Pls post sample input and expected output.
# 3  
Old 10-03-2011
if "-f" option is not in grep ..
Code:
$ xargs < key.txt | sed 's, ,|,g;s,^,egrep -i ",g;s,$,\" main.txt,g'| sh

# 4  
Old 10-03-2011
@rangarasan : Here are the sample files
Quote:
KEY_FILE : 201110030000123122331100065
MAIN_FILE:
01010001010 01010 MainFile 92929 201110030000123122331100065
01010001010 01010 MainFile 92929201110030000123122331100065
The keyword is numeric. Lines in Main File are alphanumeric and contain spaces as well.

I guess i am getting the result corectly, but the problem could be with the output being overwritten by grep line everytime. Is there a way to append every line being produced by the grep command ?
# 5  
Old 10-03-2011
Hi,

Please use >> direction instead of >

> - remove existing data
>> - append with existing data

Code:
#!/bin/ksh
KEY_FILE=KeyFile.txt
MAIN_FILE=MainFile.txt
OUTPUT_FILE=Output.txt
while read inputline
do
column=`echo "$inputline" | awk '{print $0}'`
grep "$column" $MAIN_FILE >> $OUTPUT_FILE
done < $KEY_FILE

cheers,
RangaSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to extract value after keyword in html

Using awk to extract value after a keyword in an html, and store in ts. The awk does execute but ts is empty. I use the tag as a delimiter and the keyword as a pattern, but there probably is a better way. Thank you :). file <html><head><title>xxxxxx xxxxx</title><style type="text/css"> ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk join lines based on keyword

Hello , I will need your help once again. I have the following file: cat file02.txt PATTERN XXX.YYY.ZZZ. 500 ROW01 aaa. 300 XS 14 ROW 45 29 AS XD.FD. PATTERN 500 ZZYN002 ROW gdf gsste ALT 267 fhhfe.ddgdg. PATTERN ERE.MAY. 280 PATTERRNTH 5000 rt.rt. ROW SO a 678 PATTERN... (2 Replies)
Discussion started by: alex2005
2 Replies

3. Shell Programming and Scripting

Need to extract the word after a particular keyword throughout the file..

Hi Everyone, Need help in extracting the hostname from the below output. Expected output: DS-TESTB-GDS-1.TEST.ABC.COM DS-TESTB-GDS-2.TEST.ABC.COM .... ... /tmp $ cat -n /tmp/patchreport 1 /usr/bin/perl /admin/bin/patch/applyPatches.pl --apply_patches... (4 Replies)
Discussion started by: thiyagoo
4 Replies

4. Shell Programming and Scripting

extract lines from text after keyword

I have a text and I want to extract the 4 lines following a keyword! For example if I have this text and the keyword is AAA hello helloo AAA one two three four helloooo hellooo I want the output to be one two three four (7 Replies)
Discussion started by: stekanius
7 Replies

5. Shell Programming and Scripting

Displaying all the lines starting with some keyword

consider the contents of a file has many stuff including few stuff that i need.. so i perfromed the below function cat filename | grep "ALTER TABLE" its output is as shown below . . . . . SET @sql:=CONCAT('ALTER TABLE RecordMixProfile AUTO_INCREMENT=', @maxId) ; SET... (14 Replies)
Discussion started by: vivek d r
14 Replies

6. Shell Programming and Scripting

Merge file lines based off of keyword

Hello Everyone, I have two files I created in a format similar to the ones found below (character position is important): File 1: 21 Cat Y N S Y Y N N FOUR LEGS TAIL WHISKERS 30 Dog N N 1 Y Y N N FOUR LEGS TAIL 33 Fish Y N 1 Y Y N N FINS 43 CAR Y N S Y Y N N WHEELS DOORS... (7 Replies)
Discussion started by: jl487
7 Replies

7. Shell Programming and Scripting

Extract lines of text based on a specific keyword

I regularly extract lines of text from files based on the presence of a particular keyword; I place the extracted lines into another text file. This takes about 2 hours to complete using the "sort" command then Kate's find & highlight facility. I've been reading the forum & googling and can find... (4 Replies)
Discussion started by: DionDeVille
4 Replies

8. Shell Programming and Scripting

Delete the lines before the first instance of the keyword

I have my data something like this. I want to delete all the lines before the frist instance of the key word 'ravi kumar' aaa bbbbbb cccccc ddddd eeeee 1234 ravi kumar aaaaaa vvvvvvv 5678 ravi kumar rrrrrrr mmmmmmm I want the output as follows. 1234 ravi kumar aaaaaa... (8 Replies)
Discussion started by: rdhanek
8 Replies

9. Shell Programming and Scripting

Delete the lines after the last instance of the keyword

I have my input sometyhing like this aaa bbbbbb cccccc ddddd eeeee 1234 ravi kumar aaaaaa vvvvvvv 5678 ravi kumar rrrrrrr mmmmmmm I want the output as follows. aaa bbbbbb cccccc ddddd eeeee 1234 ravi kumar aaaaaa vvvvvvv 5678 ravi kumar (2 Replies)
Discussion started by: rdhanek
2 Replies

10. Shell Programming and Scripting

Cut lines before keyword

Hi, How to cut all lines in file before keyword? from 1 2333214 word ...... some text 2 234343 234234 word ...... some text 3 234324 324 3234 word ...... some text to 1 2333214 2 234343 234234 3 234324 324 3234 (4 Replies)
Discussion started by: Trump
4 Replies
Login or Register to Ask a Question