LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-26-2016, 11:07 AM   #1
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Rep: Reputation: Disabled
Grep question


I have 2 files, contains only rows. I would like to remove files from left.txt beginning lines from found.txt (separator

My command was: cut -d: -f1 found.txt | grep -vf- left.txt > remain.txt
But it doesn't work (remain.txt is an empty file). Is it a syntax error? I could not find the solution.
Could you help me, please?

found.txt contains:

00305aa421cda76cc07228111a5650a8:test1
011bbcf1eb8f65e326ebdc4f9e349c94:test2
0127f1eeffb32ed2d6001c0a3ce365f1:test3



left.txt:

4a37a393387cfef1dc8d8ee82c763003
8f55b05e6f40e6f2fb38f1a76308df64
6a8eba4925beeaf2ffa5e498f0f22c35
ae067c5c1b846ff420d94462421197c5
8d03eb07d810d9f486ca82ce2b2df106
00305aa421cda76cc07228111a5650a8
011bbcf1eb8f65e326ebdc4f9e349c94
0127f1eeffb32ed2d6001c0a3ce365f1
 
Old 10-26-2016, 11:18 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
It works here...

Check output from ' cut -d: -f1 found.txt '
 
1 members found this post helpful.
Old 10-26-2016, 11:33 AM   #3
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
This command working:

cut -d: -f1 found.txt | less
00305aa421cda76cc07228111a5650a8
011bbcf1eb8f65e326ebdc4f9e349c94
0127f1eeffb32ed2d6001c0a3ce365f1

and this command not:
cut -d: -f1 found.txt | grep -vf- left.txt | less

So I don't understand what the problem....
Maybe very simple thing...

Last edited by freeroute; 10-26-2016 at 11:35 AM.
 
Old 10-26-2016, 12:39 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Is there a reason for the '-' after the 'f' in the grep?
 
1 members found this post helpful.
Old 10-26-2016, 12:48 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Quote:
Originally Posted by freeroute View Post
This command working:

cut -d: -f1 found.txt | less
00305aa421cda76cc07228111a5650a8
011bbcf1eb8f65e326ebdc4f9e349c94
0127f1eeffb32ed2d6001c0a3ce365f1

and this command not:
cut -d: -f1 found.txt | grep -vf- left.txt | less

So I don't understand what the problem....
Maybe very simple thing...
You use 'less', that means there are more lines than those you quoted, no?
Maybe grep doesn't find exact match for all the text coming from ' cut -d: -f1 found.txt '

With the lines you gave as example, that works
 
Old 10-26-2016, 12:50 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Quote:
Originally Posted by grail View Post
Is there a reason for the '-' after the 'f' in the grep?
I thinks it tells grep to use standard input for -f option (obtain patterns from file)
 
1 members found this post helpful.
Old 10-26-2016, 01:36 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
OP you need to think about the order in which you are doing things.

Starting with:
Code:
00305aa421cda76cc07228111a5650a8:test1
011bbcf1eb8f65e326ebdc4f9e349c94:test2
0127f1eeffb32ed2d6001c0a3ce365f1:test3
Your cut will return:
Code:
00305aa421cda76cc07228111a5650a8
011bbcf1eb8f65e326ebdc4f9e349c94
0127f1eeffb32ed2d6001c0a3ce365f1
Then you tell grep to use the data contained in another file to exclude all lines of the above that are not in the file ... problem is, all the lines are in the file

What you actually want is to store the data from fist file and output lines in second file that are not mentioned ... something like:
Code:
awk -F: 'NR==FNR{_[$1];next}!($1 in a)' found.txt left.txt
 
1 members found this post helpful.
Old 10-26-2016, 01:55 PM   #8
Boucane
LQ Newbie
 
Registered: Dec 2008
Location: Montreal
Distribution: Suse, Ubuntu, Debian
Posts: 11

Rep: Reputation: 4
comm -12 left.txt found.txt
 
2 members found this post helpful.
Old 10-26-2016, 02:07 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Quote:
Originally Posted by Boucane View Post
comm -12 left.txt found.txt
Would seem to be a small problem with that option:
Code:
$ comm -12 left.txt found.txt
comm: file 1 is not in sorted order
 
1 members found this post helpful.
Old 10-26-2016, 02:22 PM   #10
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
I solved. Still I don't know what was the problem.

I opened and saved all the 2 files with nano. Voalaaaa everything works with the original command.
Still not understand what was the problem. The original left.txt and found.txt not my files (contains more than 10k lines). I guess maybe made by windows notepad, workpad....

I will try "comm" "awk" command too. Thank you very much for your advices.
 
Old 10-26-2016, 02:29 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
ahhh ... I took of the dash thinking it was an error. It does indeed work

You are correct that if the files were created in Windows it would mess with your results.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 question antcore99 Linux - Software 12 12-08-2010 10:45 PM
grep question HarryBoy Linux - Newbie 2 08-06-2010 10:49 AM
maybe a grep question: me210 Linux - Software 3 01-19-2007 12:13 PM
grep question vasanthraghavan Programming 3 04-23-2004 12:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:57 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration