compare files and then remove some lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare files and then remove some lines
# 1  
Old 08-04-2010
compare files and then remove some lines

Hi everyone

I have a dilemma and I'm hoping someone has an answer for me.

I have two files:

Code:
# cat masterfile
line3
line4
line5
line6
line7

Code:
# cat tempfile
line1
line2
line3
line4

I want to compare tempfile with masterfile.

If there are any rows in tempfile that are NOT present in masterfile (line1 and line2 in this example) I want to remove them from tempfile.

So the end result should be:

Code:
# cat tempfile
line3
line4

Can anyone advise me on how to accomplish this?

Many thanks

P.S. I'm using Solaris 10
# 2  
Old 08-04-2010
Hi

Code:
grep -f tempfile masterfile > temp1 && mv -f temp1 tempfile

Guru.
# 3  
Old 08-04-2010
Does ur file have only one row with n values ... or it will have more sentences ?
# 4  
Old 08-04-2010
Thanks Guru, but the -f switch doesn't work on Solaris.

This is an example of the lines that are in my files:

Code:
FMDP0001_MSH11193_ATD-AAE-F6R-CB_100803_234702_book-1_page-1.png
FMDP0001_MSH11193_ATD-AAE-F6R-CB_100803_234702_book-1_page-1.xml
FMDP0001_MSH13299_ATD-AAE-F6R-CB_100803_230354_book-1_page-1.png
FMDP0001_MSH13299_ATD-AAE-F6R-CB_100803_230354_book-1_page-1.xml



---------- Post updated at 11:25 AM ---------- Previous update was at 11:07 AM ----------

Correction:

There are two version of grep on Solaris 10

/usr/bin/grep
/usr/xpg4/bin/grep

The -f switch works on the second one.

Thank you for the solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare lines between two files

I have two files I need to compare these two files and take the lines that are common in both the files and consider the line present in second file for my further processing I have used "Awk" along with "FNR and NR" but that is not working gawk -F= ' > FNR==NR {a=$1; next}; > ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. UNIX for Dummies Questions & Answers

Compare lines in 2 files

I have 2 files with exactly the same information (with header and separated by ";") and what I would like to do is print (for both files!) the columns that are different and also print the "key" column that is equal in the 2 files For example, if File1: key1;aaa;bbb;ccc key2;ddd;eee;fff... (4 Replies)
Discussion started by: mvalonso
4 Replies

3. Shell Programming and Scripting

Two files, remove lines from second based on lines in first

I have two files, a keepout.txt and a database.csv. They're unsorted, but could be sorted. keepout: user1 buser3 anuser19 notheruser27 database: user1,2343,"information about",field,blah,34 user2,4231,"mo info",etc,stuff,43 notheruser27,4344,"hiya",thing,more thing,423... (4 Replies)
Discussion started by: esoffron
4 Replies

4. Shell Programming and Scripting

Shell script to compare ,diff and remove betwen 2 files

Hi Friends Need your expertise. Command to check the difference and compare 2 files and remove lines . example File1 is master copy and File2 is a slave copy . whenever i change, add or delete a record in File1 it should update the same in slave copy . Can you guide me how can i accomplish... (3 Replies)
Discussion started by: ajayram_arya
3 Replies

5. Shell Programming and Scripting

compare files and remove a line from a file if first column is greater than 25

my files are as follows fileA sepearated by tab /t 00 lieferungen 00 attractiop 01 done 02 forness 03 rasp 04 alwaysisng 04 funny 05 done1 fileB funnymou120112 funnymou234470 mou3raspnhdhv rddfgmoudone1438748 so all those record which are greater than 3 and which are not... (4 Replies)
Discussion started by: rajniman
4 Replies

6. Shell Programming and Scripting

Compare one files with strings from another + remove lines

Have two files and want to compare the content of file1 with file2. When matched remove the line. awk 'NR==FNR {b; next} !(b in $0)' file1 file2file1 1. if match 2. removefile2 1. this line has to be removed if match 2. this line has a match, remove 3. this line has no match, no removingThe... (3 Replies)
Discussion started by: sdf
3 Replies

7. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies

8. Shell Programming and Scripting

Compare two files and remove all the contents of one file from another

Hi, I have two files, in which the second file has exactly the same contents of the first file with some additional records. Now, if I want to remove those matching lines from file2 and print only the extra contents which the first file does not have, I could use the below unsophisticated... (3 Replies)
Discussion started by: royalibrahim
3 Replies

9. UNIX for Dummies Questions & Answers

Compare and Remove duplicate lines from txt

Hello, I am a total linux newbie and I can't seem to find a solution to this little problem. I have two text files with a huge list of URLS. Let's call them file1.txt and file2.txt What I want to do is grab an URL from file2.txt, search file1.txt for the URL and if found, delete it from... (11 Replies)
Discussion started by: rmarcano
11 Replies

10. Shell Programming and Scripting

Trying to compare lines in 2 files

Hello, I am new to scripting and need some help. In looking at other posts on this forum, I came up with the following logic. I cannot figure out why I am getting names of files of the current directory in my echo output. Scenario: message file has a line containing the version. Version.txt... (2 Replies)
Discussion started by: brdholman
2 Replies
Login or Register to Ask a Question