search text file for lines present in another text file and redirect output.

rupeshforu3

Disciple
Hi I am Rupesh from India and I have a system with intel i3 10th gen processor and Asus prime H 510 me motherboard. I have installed Arch Linux and it is working fine.

I have two text files say input1.txt and input2.txt and I want to search input1 file line by line present in another text file called input2.txt and redirect output to another text file missing.txt.

input1 text file consists of lines in the following pattern.

This is a video file 1[abcdef].mp4
This is a video file 2[ghijklm].mp4
This is a video file 3[nopqrst].mp4
This is a video file 4[uvwxyz].mp4

input2 text file consists of lines in the following pattern.

This is a video file 1.mp4
This is a video file 2.mp4
This is a video file 4.mp4


Note that in the second text file one line is missing ie., "This is a video file 3.mp4"

The second text file file consists of lines from first text file last few characters removed.

Now my requirement is I want a script which searches input1.txt text file for lines containing "This is a video file 1" and after that search for "This is a video file 2" and so on.

If a match is found ignore the line. If a match is not found the line must be redirected to another text file called missing.txt.

In the above case missing.txt must contain the following line

This is a video file 3[nopqrst].mp4

You can say that use comm utility or diff utility but they are not applicable in the present situation because some characters are missing in the second text file.

You can even suggest that use text editor like gedit but I have 1000 lines in first text file and 800 lines in second text file.

I want all the 200 remaining missing lines in missing.txt file.

I have a script which has while loop and reads all the lines present in second text file input2.txt.

Code:
#!/bin/bash  while read name; do        echo $name  done < input2.txt


But I don't know how to search input1 text file and how to use if construct.

At present I am reading linux operating system and utilities from the beginning and at present I completed how Linux boots and introduction to systemd. It will take time to create a script on my own.

Kindly try to provide a script which searches input1 text file and redirect the unique lines to missing.txt file.

Regards,
Rupesh.
 
I would try something like reading input1.txt line by line then grepping for each line. When there isn't a match, pipe it to missing.txt.

Something off the top of my head:
Bash:
while read -r line1; do     if ! grep -q "$line1" input2.txt; then         echo "$line1" >> missing.txt     fi done < input1.txt

This doesn't account for the extra characters in input1.txt but I'm sure you can figure that out ;)
 
Something like this might work (on mobile so can't test):
Bash:
diff -u -d -c 0 input1.txt input2.txt
This should give you the lines in input1.txt that don't exist in input2.txt and ignore the ones that have been "modified".