0

For example, the pattern file contains:

thepiratebay.org
some-url.com

Basically, lines with these patterns need to be removed from a file. How can I do that? I prefer a method that uses core utils.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Asaf Niv
  • 1
  • 1
  • 1
    `grep -vf pattern_file file` works well when the pattern list is small. For large lists, this post has a better solution: [grep -vf too slow with large files](https://stackoverflow.com/questions/42702425/grep-vf-too-slow-with-large-files). – codeforester Jan 28 '18 at 18:00
  • 2
    Possible duplicate of [Delete lines based on pattern in another file](https://stackoverflow.com/questions/16477782/delete-lines-based-on-pattern-on-another-file) – Benjamin W. Jan 28 '18 at 18:11

2 Answers2

2

Using grep:

grep -vf pattern-file data-file  > new-file
mv new-file data-file

The -v gives lines which do not contain the pattern(s)

The -f specifies a file which contains the pattern(s)

cdarke
  • 42,728
  • 8
  • 80
  • 84
0

sed can be used for this operation as well.

 sed -i '/thepiratebay.org/d;/some-url.com/d' inputfile.txt

d option deletes the line after finding /pattern/. Use, -i.bak for FreeBSD/MacOS.

-i option edits file in-place and overwrites it in one go. -i.bak does the same, but copies the original file to inputfile.txt.bak.

iamauser
  • 11,119
  • 5
  • 34
  • 52