2

For a simple example say I have list file which has the following contents

cat dog pig
dog hat
cat
clap sing
king
ca
cat hog
plate
plate
one two three
cat

I then have a 'pattern' file. With the below content

cat 
dog

What I would like to happen is a new file is created from the list file but all lines that begin with cat or dog I not copied over.

I have seen a few examples how this can work e.g.

sed '/pattern to match/d' ./infile > ./newfile

From what I have gathered sed can't handle a 'pattern input file'. Is that a similar program to sed that could achieve what I would like to do? If not could a make a program in python program that reads each line from the 'pattern' file and run the sed command against th list file e.g. multiple passes with seds

1

You can use tell grep to read its patterns from a file - with the -v (invert) switch that should do what you want

fgrep -vf patternfile listfile

From man grep

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)
  • Exactly What I wanted Cheers – Dan1676 Jan 2 '14 at 13:31
0

I am not sure whether this what you wanted or not. I have done this what you wanted by deleting the dog & cat multiple patterns and saved the modified in the new file. see the code

 sed '/cat/d; /dog/d' file1.txt > file2.txt

below here along with example in the terminal(CTRL+ALT+T)

raja@debian:~/sand$ cat file1.txt
cat dog pig
dog hat
cat
clap sing
king
ca
cat hog
plate
plate
one two three
cat
raja@debian:~/sand$ sed '/cat/d; /dog/d' file1.txt > file2.txt
raja@debian:~/sand$ cat file2.txt
clap sing
king
ca
plate
plate
one two three
raja@debian:~/sand$ 
  • I knew about this method. Should of been a bit clearer in the question. The files I am going to run this against contains thousands of patterns thus it is impossible to write each individual out each pattern. This is why I need to use a 'pattern input file' – Dan1676 Jan 2 '14 at 13:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.