Deleting Multiple Lines in a File1 using critera found from File 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting Multiple Lines in a File1 using critera found from File 2
# 1  
Old 12-07-2012
Deleting Multiple Lines in a File1 using critera found from File 2

Hi Everyone!

I would like ask if there's a better way I can delete multiple lines in a file1 by collecting all criteria from file2.

file1:
Code:
a
b
c
d
e
f


file2:
Code:
a
e
f

The expected value will be:
Code:
b
c
d

The code below seems to work however if is taking much time when dealing with huge records.
Code:
cat file2|while read letter
do
    sed -e "/$letter/d" file1 > file1.temp
    mv file1.temp file1
done

Appreciate all your help/input in advance. Smilie

Last edited by Franklin52; 12-08-2012 at 05:11 PM.. Reason: Code tags
# 2  
Old 12-07-2012
try:
Code:
awk 'NR==FNR{a[$0]=$0;next}!a[$0]{print}' file2 file1

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 12-07-2012
Quote:
Originally Posted by rdrtx1
try:
Code:
awk 'NR==FNR{a[$0]=$0;next}!a[$0]{print}' file2 file1

wow!! this works like a charm man. thanks a lot for this it really helped me a lot.

---------- Post updated at 03:14 AM ---------- Previous update was at 02:19 AM ----------

Quote:
Originally Posted by rdrtx1
try:
Code:
awk 'NR==FNR{a[$0]=$0;next}!a[$0]{print}' file2 file1

Sorry another question.. How can I modify the above command when using the below format of file1?

File1:
a,x
b,x
c,y
d,y
e,z


File2:
x
z

Output:
c,y
d,y
# 4  
Old 12-07-2012
try:
Code:
awk 'NR==FNR{a[$0]=$0;next}!a[$2]{print}' file2.txt FS="[, *]" file1.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print matching lines in files that meet critera

In the tab delimited files below I am trying to match $2 in file1 to $2 of file2. If a match is found the awk checks $3 of file2 and if it is greater than 40% and $4 of file2 is greater than 49, the line in file1 is printed. In the desired output line3 of file1 is not printed because $3 off file2... (9 Replies)
Discussion started by: cmccabe
9 Replies

2. Shell Programming and Scripting

Deleting all the N lines after the line in which text is found.

Hi! I want to delete N (say 10) lines after the line which text is found in a file "A".Also to delete the line in which the text is found. Only one occurrence of the search string in the file "A" The text to be deleted is in another text file "B". All the search texts in the file "B" are in... (3 Replies)
Discussion started by: shahid1632
3 Replies

3. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

5. UNIX for Dummies Questions & Answers

find lines in file1.txt not found in file2.txt memory problem

I have a diff command that does what I want but when comparing large text/log files, it uses up all the memory I have (sometimes over 8gig of memory) diff file1.txt file2.txt | grep '^<'| awk '{$1="";print $0}' | sed 's/^ *//' Is there a better more efficient way to find the lines in one file... (5 Replies)
Discussion started by: raptor25
5 Replies

6. UNIX for Dummies Questions & Answers

Deleting Multiple Lines in Hosts File

Hello all, I'm using the Bash shell on Solaris 8. Please can someone tell me how I can delete multiple lines in the hosts file? I have a list of hosts that I want to quickly delete in the hosts file, but I'm looking for a quicker way than using VI to delete the lines one by one. Regards,... (4 Replies)
Discussion started by: wthomas
4 Replies

7. Shell Programming and Scripting

Multiple lines into one line when ; is found

I have sql's in a file separated by ";", need to put the sql in one single line till i find a ";" The input file is like this SELECT s.sid, s.serial#, p.spid as "OS PID", s.username, s.module, ... (6 Replies)
Discussion started by: ilugopal
6 Replies

8. Shell Programming and Scripting

Deleting multiple lines from file

Hi, I have 10 different strings. I have to delete the whole line matching with any one string. I can use sed like below sed '/$keyword1/d' fileList.txt > temp_fileList.txt sed '/$keyword2/d' temp_fileList.txt > temp_fileList1.txt . . . Here is the problem i do not have fixed number... (9 Replies)
Discussion started by: shekhar_v4
9 Replies

9. Shell Programming and Scripting

deleting lines from multiple text files

I have a directory full of text data files. Unfortunately I need to get rid of the 7th and 8th line from them all so that I can input them into a GIS application. I've used an awk script to do one at a time but due to the sheer number of files I need some kind of loop mechanism to automate... (3 Replies)
Discussion started by: vrms
3 Replies

10. Shell Programming and Scripting

Deleting Multiple Lines with sed

I am trying to use sed to delete multiple lines in a file. The problem is that I need to search for a certain line and then once found delete it plus the next 4 lines. For instance if I had a file that consisted of the following lines: #Data1.start ( (Database= data1) (Name = IPC)... (1 Reply)
Discussion started by: rambo15
1 Replies
Login or Register to Ask a Question