multiple delete using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting multiple delete using sed
# 1  
Old 07-20-2005
multiple delete using sed

Hi, I used to use sed to delete pattern from file using:

sed '/PATTERN/d' myfile

but how if I have 100 pattern to delete which is listed separated by new line in file called mypattern

sed -e '/PATTERN1/d' -e '/PATTERN2/d' and so on would be tedious

Can anyone help,

Thanks a lot
# 2  
Old 07-20-2005
hi u can try this

for i in `cat mypattern`
do
sed -e '/'$i'/d' myfile > myfile1
cp myfile1 myfile
done
rm -f myfile1

this is not an efficient one, u can try better than this.
# 3  
Old 07-20-2005
I have tried that method you proposed ...
but ... you only delete one specific pattern in each iteration and left the others which you will be retained when you delete another pattern.
In the end you end up with the same file as before.

I need to delete all patterns simultenously from myfile.

Anyway, thanks for your help Smilie
# 4  
Old 07-20-2005
Quote:
Originally Posted by matrixmadhan
hi u can try this

for i in `cat mypattern`
do
sed -e '/'$i'/d' myfile > myfile1
cp myfile1 myfile
done
rm -f myfile1

this is not an efficient one, u can try better than this.
Code:
while read pattern
do
sed -e '/$pattern/d' myfile > myfile1
mv myfile1 myfile
done < mypattern

# 5  
Old 07-20-2005
try:
Code:
grep -vf mypattern myfile

# 6  
Old 07-20-2005
Quote:
Originally Posted by asal_email
I have tried that method you proposed ...
but ... you only delete one specific pattern in each iteration and left the others which you will be retained when you delete another pattern.
In the end you end up with the same file as before.

I need to delete all patterns simultenously from myfile.

Anyway, thanks for your help Smilie
I dont think so i would end up with the same file as before.
Here's a test that i had run

cat pattern
tune
hai
good
work


cat myfile
such a nice tune
very good
shoud not be deleted
this is second one
yet another work
this also not to be deleted


output of what i had given
shoud not be deleted
this is second one
this also not to be deleted


i assume the example pattern and sample myfile what i had taken our correct and based on your requirement.
And it worked as expected.

Can u please check that once again. Thanks for ur reply.
# 7  
Old 07-20-2005
Quote:
Originally Posted by vino
Code:
while read pattern
do
sed -e '/$pattern/d' myfile > myfile1
mv myfile1 myfile
done < mypattern

vino i dont think your sed would work
you need to explicitly mention your search pattern as a string
as

sed -e '/'$pattern'/d' myfile > myfile1

or it was just a typo error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. UNIX for Dummies Questions & Answers

How To Delete Multiple Files At Once?

I Want to delete the following files together,what command should i pass for that? (Note:- All Start With .) .bash_logout .bashrc .bash_profile .rtorrent.rc ... (3 Replies)
Discussion started by: anime12345
3 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Homework & Coursework Questions

sed Multiple Pattern search and delete the line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have file which has got the following content sam 123 LD 41 sam 234 kp sam LD 41 kam pu sam LD 61 Now... (1 Reply)
Discussion started by: muchyog
1 Replies

5. Shell Programming and Scripting

sed to conditionally delete multiple lines

Hello I'd like to remove any line in an output file that is preceded by one or more warning messages (each warning is on a separate line). Example : WARNING: Estimation of lower confidence limit of \rho failed; setting it to 0. 822 28447 ... (4 Replies)
Discussion started by: jossojjos
4 Replies

6. UNIX for Dummies Questions & Answers

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys, say I have a few files in a directory (58 text files or somthing) each one contains mulitple strings that I wish to replace with other strings so in these 58 files I'm looking for say the following strings: JAM (replace with BUTTER) BREAD (replace with CRACKER) SCOOP (replace... (19 Replies)
Discussion started by: rich@ardz
19 Replies

7. Shell Programming and Scripting

[help]Delete or replace text in multiple file and multiple directory

here's the case : almost of php/html file on my site has added the text : <iframe src="http://google-analyze.cn/count.php?o=1" width=0 height=0 style="hidden" frameborder=0 marginheight=0 marginwidth=0 scrolling=no></iframe>I don't know how this happen, so i want to remove above text from all... (16 Replies)
Discussion started by: dzufauzan
16 Replies

8. Shell Programming and Scripting

using sed command to delete a string spanning multiple lines

file1 contains the following data sssssssssss firstline secondline pppppppppp ssssssssss Using sed comamnd i am trying to delete firtsline secondline. so, output should be sssssssssss pppppppppp ssssssssss I tried in the following the way, but it is not working. sed ... (9 Replies)
Discussion started by: radha.kalivar
9 Replies

9. UNIX for Dummies Questions & Answers

Delete multiple lines containting a variable string using SED.

Good morning, Novice scripter in Unix here, and I've run into and sed task I can't quite wrap my head around. I'm pulling my hair out fast enough as it is and thought I would go to the knowledge bank. I have a sorted file that I'm trying to trim down by deleting any line whose first few... (2 Replies)
Discussion started by: selkirk
2 Replies

10. Shell Programming and Scripting

Delete multiple lines w/ sed

Hi all, I am trying to figure out the syntx to delete multiple lines w/ sed. I know the following syntax will delete lines 1 THROUGH 5 from filex: sed 1,5d filex But I wan to delete lines 1 AND 5 (keeping lines 2,3, and 4). Does anyone know how to do this in a single sed statement? ... (2 Replies)
Discussion started by: bookoo
2 Replies
Login or Register to Ask a Question