Search

script to delete the lines from a file having patterns present in another file

Let us assume we have set of patterns in a file as shown below.

data1:



And a file with the contents

data2:



If we want to search for the patterns in the file data1 and delete the lines that have the pattern in the file data2, we can do it using the following script.

del_pattern.sh :



save it in the same directory where the above two text files are saved,give it execute permission and execute it.



The while loop, loops through the file data1, reading on line at a time. The sed command in the while loop searches for the pattern in the file data2 and any line that has the pattern will be substituted by a blank line.

Instead of replacing with a blank line, we can delete the line using the following script.

del_pattern.sh



save it in the same directory where the above two text files are saved,give it execute permission and execute it.




Follow by Email