linux - filter out content between two files


Question: 

I have two files

conditions.txt

abcd
efgh

logs.txt

efgh
ijkl
mnop
qrst

I am expecting output to be:

ijkl
mnop
qrst

Actual output:

efgh
ijkl
ijkl
mnop
mnop
qrst
qrst

Here's the code I had worked till now

func(){
while read condition
do
if [[ $line = $condition ]] ; then
:
else
echo "$line";
done < condition.txt
}

while read line
do
func $line
done < log.txt



2 Answers: 

Try using grep:

$ grep -v -f conditions.txt logs.txt

From the man page for GNU grep:

-v, --invert-match
          Invert the sense of matching, to select non-matching lines.

-f FILE, --file=FILE
          Obtain patterns from FILE, one per line.  If this option is used multiple times or is combined with the -e (--regexp)  option,  search  for  all  patterns
          given.  The empty file contains zero patterns, and therefore matches nothing.
 

If you don't feel like re-inventing wheels ...

grep -vf conditions.txt logs.txt 
ijkl
mnop
qrst
 

More Articles


ruby on rails - tmux: are my other terminals still running?

I have started using a certain technique for launching my web code since I switched server setups. I type tmuxsudo startserverrailsCtrl+B then DThen to reboot I type tmuxsudo killall rubysudo startserverrailsCtrl+B then DMy questions are: when I kill the ruby, does that end the session that I starte

node.js - Not able to run my node app at port 80 on ubuntu 16.04

I want to run my node app at port 80 in ubuntu 16.04. But the moment I'm trying to run my app with npm start its throwing error Port in already in use whereas it's not in use. According to Github Reply, We need root privileges to run on ports below 1048 which is correct.So the moment I'm trying to r

shell - Move output of Find command unix

Im learning unix programming , i want to find all files whose size is greater than 1M an print them into a file.here is my code find. -size +1M -print0 | xargs -I -O '{}' mv '{}' files all sites i have found refer to this one as right one , but it does not work . currently im working on ubuntu


php - Ubuntu Server Virtual Host not redirecting properly

I have installed Web site on my Ubuntu Server. The following things are installed on my Server for running web service. 1) Apache22) PHP53) MySql My main website is redirected to https.Example (changed for security purpose) <VirtualHost *.80>ServerName www.example.comServerAlias example.co

shell - UNIX: copy entire directory structure and only specific file type

How i can recreate entire directory structure and copy only files with .txt extension if present. I would like to copy the directory or sub-directory even if its empty.

c - Why do I get a seg fault on Ubuntu but not mac?

I have a program that checks the modification time of a file and executes the file if it has changed. Currently it works if I run it on my mac, but it seg faults if I run it on ubuntu. Please help me.note: this is in c#include <unistd.h>#include <stdlib.h>#include <stdio.h>#inclu


php - Page not found error on apache server on ubuntu 14.04

I have a project which i developed using WAMP. The project works fine as expected. I tried to use the same project on LAMP stack (Ubuntu 14.04). I imported the database to mysql server and then copied the files to /var/www/html folder and gave permissions using chmod to all files in www folder. When

linux - Run a intotify sricpt in background

how can i run 12 bash scripts simultaniously in the backgroud?That's the script:#/bin/bashwhile inotifywait -e close_write /var/www/html/wp-content/QM-Handbuch /01-Inhaltsverzeichnis.doc; do soffice --headless --convert-to pdf /var/www/html/wp-content/QM-Handbuch/01-Inhaltsverzeichnis.doc &

mysql - How to install older PHP version in XAMPP on Ubuntu 16.04?

I'm trying to install Magento on my system, But it gives me an error that the PHP version that I have is newer and Magento is not supported that version. So now I'm trying to downgrade my PHP version from XAMPP that I'm installed on Ubuntu 16.04.Error : PHP Version Check Your PHP version is 7.1.1.

c - Printf ubuntu Segmentation fault (core dumped)

I have this code:int a;printf("&a = %u\n",(unsigned)&a);printf("a\n");printf("b\n");printf("c\n");printf("d\n");I tried to print the pointer of a variable.But it fail on the row printf("a\n"); and says Segmentation fault (core dumped)Output:&a = 134525024Segmentation fault (core dumped)W