Skip to main content
added 3 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Here's another way:

grep -vFxf new.txt old.txt >> new.txt 

The -v flag tells grep to print lines that do NOT match the pattern given, and the -f new.txt tells grep to use the lines in the file new.txt as the patternpatterns to search for. With -F, the patterns are Fixed strings instead of basic regular expressions and with -x, they have to match the full lines. So grep -vFxf new.txt old.txt means "show me all lines in old.txt that are missing from new.txt".

We append those lines to new.txt using the >> redirection operator. Note that new.txt is used here both as input and as inputoutput, but grep will read the full initial contentcontents of new.txt to build the list of strings to search and stop reading it after that, so extra lines printed afterwards to new.txt won't be read again.

Here's another way:

grep -vFxf new.txt old.txt >> new.txt 

The -v flag tells grep to print lines that do NOT match the pattern given, and the -f new.txt tells grep to use the lines in the file new.txt as the pattern to search for. With -F, the patterns are Fixed strings instead of basic regular expressions and with -x, they have to match the full lines. So grep -vFxf new.txt old.txt means "show me all lines in old.txt that are missing from new.txt".

We append those lines to new.txt using the >> redirection operator. Note that new.txt is used here both as input and as input, but grep will read the full initial content of new.txt to build the list of strings to search and stop reading it after that, so extra lines printed afterwards to new.txt won't be read again.

Here's another way:

grep -vFxf new.txt old.txt >> new.txt 

The -v flag tells grep to print lines that do NOT match the pattern given, and the -f new.txt tells grep to use the lines in the file new.txt as the patterns to search for. With -F, the patterns are Fixed strings instead of basic regular expressions and with -x, they have to match the full lines. So grep -vFxf new.txt old.txt means "show me all lines in old.txt that are missing from new.txt".

We append those lines to new.txt using the >> redirection operator. Note that new.txt is used here both as input and as output, but grep will read the full initial contents of new.txt to build the list of strings to search and stop reading it after that, so extra lines printed afterwards to new.txt won't be read again.

added 353 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Here's another way:

grep -vfvFxf new.txt old.txt | tee -a>> new.txt 

The -v flag tells grep to print lines that do NOT match the pattern given, and the -f new.txt tells grep to use the lines in tethe file new.txt as the pattern to search for. With -F, the patterns are Fixed strings instead of basic regular expressions and with -x, they have to match the full lines. So grep -vfvFxf new.txt old.txt means "show me all lines in old.txt that are missing from new.txt".

We then pipe thisappend those lines to teenew.txt withusing the -a>> flag which means "append"redirection operator. Note that new.txt is used here both as input and as input, but grep will read the full initial content of new.txt to build the list of strings to search and tellstop reading it to add theseafter that, so extra lines printed afterwards to new.txt won't be read again.

Here's another way:

grep -vf new.txt old.txt | tee -a new.txt 

The -v flag tells grep to print lines that do NOT match the pattern given, and the -f new.txt tells grep to use the lines in te file new.txt as the pattern to search for. So grep -vf new.txt old.txt means "show me all lines in old.txt that are missing from new.txt.

We then pipe this to tee with the -a flag which means "append", and tell it to add these lines to new.txt.

Here's another way:

grep -vFxf new.txt old.txt >> new.txt 

The -v flag tells grep to print lines that do NOT match the pattern given, and the -f new.txt tells grep to use the lines in the file new.txt as the pattern to search for. With -F, the patterns are Fixed strings instead of basic regular expressions and with -x, they have to match the full lines. So grep -vFxf new.txt old.txt means "show me all lines in old.txt that are missing from new.txt".

We append those lines to new.txt using the >> redirection operator. Note that new.txt is used here both as input and as input, but grep will read the full initial content of new.txt to build the list of strings to search and stop reading it after that, so extra lines printed afterwards to new.txt won't be read again.

Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

Here's another way:

grep -vf new.txt old.txt | tee -a new.txt 

The -v flag tells grep to print lines that do NOT match the pattern given, and the -f new.txt tells grep to use the lines in te file new.txt as the pattern to search for. So grep -vf new.txt old.txt means "show me all lines in old.txt that are missing from new.txt.

We then pipe this to tee with the -a flag which means "append", and tell it to add these lines to new.txt.