Deleting multiple lines from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting multiple lines from file
# 1  
Old 03-10-2009
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 of strings and I want to eliminate the temp files creation.

Is there any way to achieve this.

Thanks,
Shekhar
# 2  
Old 03-10-2009
If you have the strings in a file you can use grep with the -f and -v option.

Regards
# 3  
Old 03-10-2009
Hi Franklin,

Can you tell me the syntax to use grep with -v & -f. I have strings to be matched in a file.
# 4  
Old 03-10-2009
Code:
grep -v -f file_with_strings_in_it  fileList.txt

# 5  
Old 03-10-2009
I tried with the below command

grep -v -f str.txt list.txt

Its not working, saying grep usage not allowed with -f.


Is there any other way to acheive the task.


Thanks,
Shekhar
# 6  
Old 03-10-2009
Supposing the lines of list.txt match exactly the lines you want to delete in file.txt:

Code:
awk 'NR==FNR{a[$0];next} !($0 in a)' list.txt file.txt

Regards
# 7  
Old 03-10-2009
in case your sed supports the in-line edit option:
Code:
for keyword in `cat str.txt`; do sed -i -e '/'$keyword'/d' list.txt; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Shell Programming and Scripting

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: a b c d e f file2: a e f The expected value will be: b (3 Replies)
Discussion started by: zzavilz
3 Replies

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

4. Shell Programming and Scripting

pattern matching over multiple lines and deleting the first

I've got a longish log file with content such as Uplink traffic: Downlink traffic: I want to parse the log file and remove any line that contains the string "Uplink traffic:" at the beginning of the line, but only if the line following it beginnings with the string "Downlink traffic:" (in... (7 Replies)
Discussion started by: Yorkie99
7 Replies

5. UNIX for Advanced & Expert Users

Deleting lines from a file

How I can delete 100 lines anywhere in a file without opening a file and without renaming the file. (11 Replies)
Discussion started by: Nirgude07
11 Replies

6. UNIX for Dummies Questions & Answers

Deleting whole lines from a file

I have a file with 65 sets of 35 coordinates, and would like to isolate these coordinates so that I can easily copy the coordinates to another file. The problem is, I've got a 9 line header before each set of coordinates (so each set is 44 lines long). There are a zillion threads out there about... (3 Replies)
Discussion started by: red baron
3 Replies

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

8. Shell Programming and Scripting

Deleting lines in a file

How do I delete all the lines after the line containing text ***DISCLOSURES*** . I want to delete this line too. Thank you (2 Replies)
Discussion started by: reachsamir
2 Replies

9. Shell Programming and Scripting

Deleting last 2 lines from the file.

Hi I have a file & always I need to remove or delete last 2 lines from that file. So in a file if I have 10 lines then it should return me first 8 lines. Can someone help me? (4 Replies)
Discussion started by: videsh77
4 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