Compare one files with strings from another + remove lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare one files with strings from another + remove lines
# 1  
Old 12-19-2011
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.
Code:
awk 'NR==FNR {b[$0]; next} !(b in $0)' file1 file2

file1
HTML Code:
1. if match
2. remove
file2
HTML Code:
1. this line has to be removed if match
2. this line has a match, remove
3. this line has no match, no removing
The code does not remove the matches.
# 2  
Old 12-19-2011
What exactly are you matching? None of those lines match...

If your grep supports -f then grep -vf file1 file2 (or vice-versa) may do what you want.
# 3  
Old 12-19-2011
Quote:
Originally Posted by CarloM
What exactly are you matching? None of those lines match...

If your grep supports -f then grep -vf file1 file2 (or vice-versa) may do what you want.
Two lines match and need to be removed with awk

negativlist
HTML Code:
if match
remove
input
HTML Code:
1. this line has to be removed if match
2. this line has a match, remove
3. this line has no match, no removing
output
HTML Code:
3. this line has no match, no removing
# 4  
Old 12-20-2011
This is what i was looking for. It prints only lines which do NOT match strings stored in a seperate file.

Code:
awk -FS=" " 'NR==FNR {b[$0]; next} {for (x in b) if($0 ~ x) next;print $0}'

Took me some hours but finally got the code.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove lines containing 2 or more duplicate strings

Within my text file i have several thousand lines of text with some lines containing duplicate strings/words. I would like to entirely remove those lines which contain the duplicate strings. Eg; One and a Two Unix.com is the Best This as a Line Line Example duplicate sentence with the word... (22 Replies)
Discussion started by: martinsmith
22 Replies

2. Shell Programming and Scripting

How to remove lines containing strings

Hi Guys, I need some script in removing lines containing strings like ",, ," and "rows". Retain only numbers as the output. See below for the input and output file. INPUT FILE: 9817 9832 6285 6312 6313 6318 ,, , 6329 7078 7098 7130 7959 7983 (7 Replies)
Discussion started by: pinpe
7 Replies

3. Shell Programming and Scripting

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: # cat masterfile line3 line4 line5 line6 line7 # cat tempfile line1 line2 line3 line4 I want to compare tempfile with masterfile. (3 Replies)
Discussion started by: soliberus
3 Replies

4. Shell Programming and Scripting

Match strings in two files and compare columns of both

Good Morning, I was wondering if anybody could tell me how to achieve the following, preferably with a little commenting for understanding. I have 2 files, each with multiple rows with multiple columns. I need to find each row where the value in column 1 of file 1 matches column 1... (10 Replies)
Discussion started by: GarciasMuffin
10 Replies

5. Shell Programming and Scripting

Compare two strings, and print lines containing mismatches

pls help me on this... and im really sorry because i really don't know where to start here... FILE1 ABC DEF 10 2 DEF GHI 11 3 GHI JKL 12 5 JKL MNO 13 7 MNO PQR 14 5 requirements: 1. The third string should only be 10 or 12 2. The fourth string should only be 2 or 3 3. Prinnt... (1 Reply)
Discussion started by: kingpeejay
1 Replies

6. 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

7. Shell Programming and Scripting

different take on common ?: search for two strings and remove lines between them

Thank you for assisting, I've got a partial solution just needs a tweak. Hulk-BASH$ cat somefile.txt oh there is some stuff here some more stuff here START_LABEL stuff I want more stuff I want END_LABEL other stuff here too and even more stuff here too Hulk-BASH$ Hulk-BASH$ sed... (8 Replies)
Discussion started by: laser
8 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. Shell Programming and Scripting

Remove matching lines with list of strings

Hi, HP-UX gxxxxxxxc B.11.23 U ia64 3717505098 unlimited-user license I have a file with below pipe separated field values: xxx|xxx|abcd|xxx|xxx|xx xxx|xxx|abcd#123|xxx|xxx|xx xxx|xxx|abcd#345|xxx|xxx|xx xxx|xxx|pqrs|xxx|xxx|xx xxx|xxx|pqrs#123|xxx|xxx|xx The third field has values like... (6 Replies)
Discussion started by: Nanu_Manju
6 Replies

10. 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
Login or Register to Ask a Question