Substraction of matching lines from a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substraction of matching lines from a file.
# 1  
Old 08-26-2013
Wrench Substraction of matching lines from a file.

I have 2 files:
file1.txt contains

Code:
/html/mybook/Charts/143712/reptiles.pdf
/html/mybook/Charts/198459/spices.pdf
/html/mybook/Charts/198459/fresh_nuts.pdf
/html/mybook/Charts/123457/dome_anim.pdf
/html/mybook/Charts/123457/vegetables.pdf
/html/content/3DInteractive/174091/CSPSGGB.html

file2.txt contains

Code:
/html/mybook/Charts/123457/
/html/content/3DInteractive/174091/

I need out put file1.txt as (without creating a new file).

Code:
/html/mybook/Charts/143712/reptiles.pdf
/html/mybook/Charts/198459/spices.pdf
/html/mybook/Charts/198459/fresh_nuts.pdf

file1.txt should contain the lines after the subtraction of lines in file2.txt, It will be very helpful for me, if you have a solution for this.

Last edited by Scrutinizer; 08-26-2013 at 03:55 AM.. Reason: code tags
# 2  
Old 08-26-2013
Try:
Code:
grep -vf file2 file1

To put the changes in the original file, create a new file and if the result is OK replace the original with the new file..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-27-2013
But this doesn't meets my requirement.
In my case the file1.txt file should be the out put after the subtraction of the lines as (file1.txt-file2.txt=file1.txt).
# 4  
Old 08-27-2013
You can probably do this if it is okay to create another file and then move it to original file

Code:
grep -vf file2 file1 >file3
mv file3 file1

This User Gave Thanks to krishmaths For This Post:
# 5  
Old 08-27-2013
But my intention is with out creating a new file like file3.txt.
# 6  
Old 08-27-2013
You could copy the input file first and use copy as input and create the original file as output..

Last edited by Scrutinizer; 08-27-2013 at 02:51 AM..
# 7  
Old 08-27-2013
also, try something like:
Code:
awk 'NR==FNR {a[$0]=$0; next; } {s=$0; sub("[^/]*$","", s); if (!a[s]) print}' file2.txt file1.txt |&
wait
while read -p x ; do echo "$x" ; done > file1.txt


Last edited by rdrtx1; 08-27-2013 at 04:34 PM.. Reason: ksh example
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to average matching lines in file

The awk below executes and is close (producing the first 4 columns in desired). However, when I add the sum of $7, I get nothing returned. Basically, I am trying to combine all the matching $4 in f1 and output them with the average of $7 in each match. Thank you :). f1 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to combine matching lines in file

I am trying to combine all matching lines in the tab-delimited using awk. The below runs but no output results. Thank you :). input chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 47433390 47433999 SYN1... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

4. Shell Programming and Scripting

Print matching lines in a file

Hello everyone, I have a little script below: die "Usage infile outfile reGex" if @ARGV != 3; ($regex) = @ARGV; open(F,$ARGV) or die "Can't open"; open(FOUT,"+>$ARGV") or die "Can't open"; while (<F>) { print FOUT if /$regex/.../$regex/; } No matter what I give $regex on the... (2 Replies)
Discussion started by: new bie
2 Replies

5. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

6. Shell Programming and Scripting

Print lines matching value(s) in other file using awk

Hi, I have two comma separated files. I would like to see field 1 value of File1 exact match in field 2 of File2. If the value matches, then it should print matched lines from File2. I have achieved the results using cut, paste and egrep -f but I would like to use awk as it is efficient way and... (7 Replies)
Discussion started by: SBC
7 Replies

7. Shell Programming and Scripting

How to print file without few exactly matching lines?

Hi I have a very long file with 4 columns of numbers for example 1875 1876 12725 12723 13785 13786 4232 4230 13184 13185 ... (2 Replies)
Discussion started by: ananyob
2 Replies

8. Shell Programming and Scripting

delete lines in file matching a pattern

I have a text file, a sample of which is as follows: r/- * 0: WINDOWS/Microsoft.NET/Framework/v2.0.50727/ASP.NETWebAdminFiles/Images/headerGRADIENT_Tall.gif r/- * 0: WINDOWS/SoftwareDistribution/Download/cf8ec753e88561d2ddb53e183dc05c3e/backoff.jpg r/- * 0: ... (2 Replies)
Discussion started by: stumpyuk
2 Replies

9. Shell Programming and Scripting

Reading lines in a file matching a pattern

Hi, I need to redirect the lines in a file to a different file if the character starting from 2 to 6 in the line are numerical . Please let me know if anyone have any script to do this. Thanks, Ranjit (4 Replies)
Discussion started by: torenji
4 Replies
Login or Register to Ask a Question