fgrep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting fgrep command
# 1  
Old 12-23-2009
fgrep command

How can we use fgrep command to search pattern on perticular field on a file.
eg : I have a parren file having format
cat patternfile
SPA16S199982
SPA5S26330

I want to seach these pattern on FIRST field of File2(since other field has
same data) and that too maching entirely.

cat File2
SPA16S199982P|Diamond Necklace|SPA16S199982|1
SPA16S199982|Diamond Necklace|SPA16S199982|1
SPA5S26330|Diamond Necklace|SPA16S199982|1


Desired O/p
----------
SPA16S199982|Diamond Necklace|SPA16S199982|1
SPA5S26330|Diamond Necklace|SPA16S199982|1

The command that i am using is fgrep -f -x patternfile File2. How to modify this command for grepping on first column.


Thanxx in advance
# 2  
Old 12-23-2009
if using fgrep is not a compulsion:

Code:
while read LINE;do grep -w "^$LINE" File2;done < patternfile

or with awk this should also work

Code:
awk -F"|" 'BEGIN{while(getline<"patternfile") arr[$1]=1 } ; arr[$1] ==1 {print $0 } ' File2


Last edited by clx; 12-23-2009 at 03:47 AM.. Reason: added awk soln
# 3  
Old 12-23-2009
You can not use fgrep for this purpose, you have to use grep. Also you can not use the -x option since that matches the whole line, plus you can not use the pattern unmodified. So you either have to modify the pattern file or modify the patterns on the fly:
Code:
grep -wf <(sed s'/^/^/' patternfile) File2

or use awk:
Code:
awk -F '|' 'NR==FNR{A[$1]=1;next}A[$1]' patternfile File2


Last edited by Scrutinizer; 12-25-2009 at 01:40 PM..
# 4  
Old 12-23-2009
did you mean word match ?

Code:
13:17:45 : tmp :cat pat
SPA16S199982
SPA5S26330
13:17:46 : tmp :fgrep -w -f pat example
SPA16S199982P|Diamond Necklace|SPA16S199982|1
SPA16S199982|Diamond Necklace|SPA16S199982|1
SPA5S26330|Diamond Necklace|SPA16S199982|1

Or if you wanted only the first field to match, then give ^ at the pattern as

Code:
$ cat pat
^SPA16S199982
^SPA5S26330

$ grep -w -f pat example
SPA16S199982|Diamond Necklace|SPA16S199982|1
SPA5S26330|Diamond Necklace|SPA16S199982|1

# 5  
Old 12-23-2009
Hi All, the awk command is working fine Thanks a lot .Now the major think is my pattern file and main file has 5million data. In that case will the awk command be faster ?
# 6  
Old 12-23-2009
If the awk command can handle the 5M records I think it will be plenty fast.
# 7  
Old 12-23-2009
Code:
#!/bin/sh

PATTERNS=`cat patternfile`

for pat in $PATTERNS
do
    cat File2 | grep "^$pat\>"
done


Last edited by pludi; 12-23-2009 at 09:19 AM.. Reason: code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need to use fgrep and egrep in a single command

I have a source file which is something like : C/*M/ / ***HMACCT ** MONTH FOR CURRENT MINUS 14 CAL DAY C/*D/ / ***HMACCT ** DAY FOR CURRENT MINUS 14 CAL DAY C/*X/ / ***HMACCT ** CENTURY FOR CURRENT MINUS 14 CAL DAY C/*Y/ / ***HMACCT **... (5 Replies)
Discussion started by: gotamp
5 Replies

2. UNIX for Dummies Questions & Answers

Fgrep -v

Using the fgrep command with the -v option. I have two files: file1: dog 1 cat 3 bird 5 fish 7file2: dog catUsing fgrep -v file2 file1 According to the fgrep man page, the output should be bird5 fish 7 but I can't seem to get it to work. Any help would be appreciated. (2 Replies)
Discussion started by: jimmyf
2 Replies

3. Shell Programming and Scripting

fgrep command: Perl programming help needed..Kindly advise

Hi, I am novice in PERL enviornment. I have a text files withso many entries in rows and columns. I have to pick up entries named as "Uniprot ID" in the file and create a new text file with list of particular Uniprot ID entries. Can anybody guide regarding this.. I came to know abut fgrep... (1 Reply)
Discussion started by: manigrover
1 Replies

4. Shell Programming and Scripting

Pipe and fgrep

Hi , Please help me resolve the below issue. I need to combine the below two command into one. grep 'ImanItemP' import.report| tr -s ' ' | cut -f2 -d ' ' > tmp.txt fgrep -v -f tmp.txt input.txt > reuired file.txt Thanks Ramesh (4 Replies)
Discussion started by: ramesh12621
4 Replies

5. Shell Programming and Scripting

Problem in Using fgrep Command with pattern file option

Hi, i am using fgrep command with following syntax fgrep -v -f pattern_file_name file file contains few line and have the pattern which i am giving in pattern file. My Problem is : its is not giving any output. while i am using fgrep -f pattern_file_name file it is showing all... (4 Replies)
Discussion started by: emresearch
4 Replies

6. Shell Programming and Scripting

Awk Vs Fgrep

Hi All, I have 2 files new.txt and old.txt cat new.txt sku1|v1|v2|v3 sku2|v11|v22|v33 sku3|v11|v22|v33 cat old.txt sku1|vx1|vx2|vx3 sku2|vx11|vx22|vx33 sku3|v11|v22|v33 The key column in both files are first column itself. I want to get records in... (6 Replies)
Discussion started by: morbid_angel
6 Replies

7. Shell Programming and Scripting

fgrep to file plus some

Hi, I know that I can grep desired data to a file but is there a way to add an additional field of data to the output in addition to what is found. I want to add a login id to the data that is found in the grep. Is this even possible? Thanks for your help. Toni (14 Replies)
Discussion started by: ski
14 Replies

8. UNIX for Dummies Questions & Answers

FGREP question

Hi, I need fgrep to search all files in the subdirectories in /var/spool/postfix/defer/... How can I issue such a command? (3 Replies)
Discussion started by: mojoman
3 Replies

9. UNIX for Dummies Questions & Answers

Usage of fgrep -f command in kshell

Hi, Can I know the usage of fgrep -f command in kshell. Reqirement: Need to search for list of patterns in list of files and display all the files which are having atleast one in the patterns list. I hope fgrep -f will serve this purpose within a loop. But I am not successfull in doing this.... (0 Replies)
Discussion started by: divv
0 Replies

10. UNIX for Advanced & Expert Users

fgrep

Hi Guys, Can anyone explain why I'm getting error below? when I execute in other server there is no error, what could be the reason. $ fgrep -f timecell.txt ldap_prev.dat wordlist too large $ wc -l timecell.txt ldap_prev.dat 372461 timecell.txt 3722878 ldap_prev.dat Thanks in... (2 Replies)
Discussion started by: krishna
2 Replies
Login or Register to Ask a Question