LinuxQuestions.org
Visit Jeremy's Blog.
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 08-27-2013, 01:13 AM   #1
jojanmpaul
Member
 
Registered: Sep 2012
Location: Bangalore
Posts: 80

Rep: Reputation: Disabled
Thumbs up Substraction of matching lines from a file.


file1.txt
/123/456/789/active/161208/avg_speed_and_avg_velocity_161208.html
/123/456/789/active/205510/210.html
/123/456/789/active/143416/CS_MM_GGB_15.htm
/123/456/789/active/174498/CS_AP_GGB_14.html
/123/456/789/active/174221/open_pipe_1741.html
/123/456/789/active/143449/images/ime007.gif
/123/456/789/active/143449/images/ime006.png
/123/456/789/active/143449/images/imag02.gif
/123/456/789/active/143449/images/Thms.db
/123/456/789/active/143449/images/ima01.png

file2.txt
/123/456/789/active/143416/
/123/456/789/active/174498/
/123/456/789/active/174221/
/123/456/789/active/143449/

output file1.txt(must be)

/123/456/789/active/161208/avg_speed_and_avg_velocity_161208.html
/123/456/789/active/205510/205510.html

My intention is file1.txt-file2.txt=file1.txt means the file1.txt itself should be the output file(if posible), don't want to be created a 3rd file for output. The repeated lines also should be deleted from the file1.txt. It is clear that if the string is matches a line the particular line should be deleted even if there are number of matching lines also.
 
Old 08-27-2013, 01:18 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
looks like a homework.
the solution depends on the size of the files.
if file2 is relatively small you can simply run grep -v line by line.
otherwise you would need to create a script: sort both files and make a loop.
 
2 members found this post helpful.
Old 08-27-2013, 01:18 AM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

have you considered using grep iwth the -v and -f options? I think this will do what you want, but it will require you to use a temporary file.

Eg.
Code:
grep -v -f file2.txt file1.txt > tmp.txt
mv tmp.txt file1.txt
Evo2.
 
Old 08-27-2013, 01:56 AM   #4
jojanmpaul
Member
 
Registered: Sep 2012
Location: Bangalore
Posts: 80

Original Poster
Rep: Reputation: Disabled
Unhappy

I neeed to delete 4000 lines file from a 2.2 lack lines file, hoping with out generating a 3rd file. The above mention is just a sample for better understanding.
 
Old 08-27-2013, 02:01 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
so you can try the solution of evo2, actually it will not create a third file (just a temporary file was created, but also removed at the end).
You can try:
Code:
cat file1.txt | grep -v -f file2.txt > file1.txt
also, if you would like to. This is not a really safe solution
 
Old 08-27-2013, 02:14 AM   #6
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
Ofcourse not a safe Solution @pan64
its good if you can do the redirect output to a third file.
 
Old 08-27-2013, 12:34 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by pan64 View Post
You can try:
Code:
cat file1.txt | grep -v -f file2.txt > file1.txt
Eliminate the cat to tighten this code a bit...
Code:
grep -vf $InFile2 $InFile1 >$OutFile
Daniel B. Martin

Last edited by danielbmartin; 08-27-2013 at 12:42 PM. Reason: Tighten the code, slightly
 
  


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
[SOLVED] bash matching two files lines by lines rperezalejo Programming 6 01-12-2012 06:42 AM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
matching array's lines?? HyperTrey Programming 7 05-29-2008 07:42 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

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

All times are GMT -5. The time now is 05:20 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
Open Source Consulting | Domain Registration