LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2013, 12:26 PM   #1
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Rep: Reputation: Disabled
Exclamation grep pattern grep -Ff


file1
Code:
hello  333
hi  333
foo  111
ok  333
egypt 11
nas  333
nop 444

file2
Code:
hi
foo
nassw
local
i am using to find matching but i need to show the remain words from file2

grep -Ff file2 file1


output should be
Code:
nassw
local

any idea

Last edited by phpshell; 11-24-2013 at 12:29 PM.
 
Old 11-24-2013, 12:42 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I don't think it's possible to show the not matched patterns using grep. What about an awk solution like this?
Code:
awk 'FNR == NR{p[$1]++} FNR < NR{if ($1 in p) delete p[$1]} END{for (i in p) print i}' file2 file1
 
Old 11-24-2013, 12:45 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
colucix was faster with the awk solution, here's a less elegant one:
Code:
comm -13 <(cut -f1 -d" " file1 | sort) <(sort file2)
local
nassw
 
Old 11-24-2013, 12:50 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
See if that works for you.

Code:
#!/bin/bash

while read p; do
        if ! grep -q $p file1; then
               echo $p
        fi
done < file2
Edit: Ok, some people are just muuuch quicker than others

Last edited by sycamorex; 11-24-2013 at 12:51 PM.
 
Old 11-24-2013, 12:52 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Code:
cut -f1 -d' ' file1 | grep -Fwvf - file2
 
2 members found this post helpful.
Old 11-24-2013, 10:21 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
As we know it is field 1 in File1, I would switch the order for the awk to be a little shorter:
Code:
awk 'FNR==NR{a[$1]}!($1 in a)' File1 File2
 
1 members found this post helpful.
Old 11-24-2013, 11:31 PM   #7
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
many thanks to you all
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
[SOLVED] grep two pattern from a file Sattee Linux - General 18 06-13-2012 04:13 AM
[SOLVED] grep with pattern danielbmartin Linux - Newbie 3 12-05-2011 10:16 PM
grep till second pattern rattlesnakejoe Programming 1 11-22-2009 08:09 AM
using grep when the pattern contains a ! farmerjoe Programming 9 03-15-2005 11:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration