• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

Compare 2 Lists

I need to take two text files. One has one column of data, one word per line. The second one has a number of words per line, but the first line (separated by white space) is the only thing I am interested in removing any lines that do not have "DD" in the word. I would like to print a list of any words that appear in 1.txt that do not appear in 2.txt. They might be in any order, and might appear any number of times. Is there an easy way of doing this?

Examples:

1.txt:
test
DDblah
DDblahBlah

2.txt:
testtest
DDBlahblah

Result:
DDblah

Since the only work that has DD in it, that appears in 1.txt that does not appear in 2.txt is DDblah, only DDlah would be printed.
0
stakor
Asked:
stakor
  • 7
  • 3
1 Solution
 
stakorAuthor Commented:
Sorry, 2.txt should read:

testtest gibberish gibberish
DDBlahblah gibberish gibberish gibberish
0
 
ozoCommented:
I'm confused.
first line (separated by white space)
Do you mean the first word  (separated by white space)?
but the first line (separated by white space) is the only thing I am interested in removing any lines that do not have "DD" in the word.
Does the I am interested in go with is the only thing am interested in or does it go with I am interested in removing any lines?

is
DDblahBlah
the same as
DDBlahblah
?
Do you want to ignore case?


Do you mean you want to do something like:
perl -d -alne '$x{lc $1}|=$#ARGV if /^(DD\S*)/;END{$x{$_}==0 and print for keys %x}' 1.txt 2.txt
0
 
stakorAuthor Commented:
Sorry for the mistake. The first word is what I was trying to say.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
stakorAuthor Commented:
And case should be ignored as well.
0
 
stakorAuthor Commented:
Is there a way to run this without invoking a database mode? I know that would probably the most efficient, but this will likely only be a list of like 50 - 100 items.
0
 
stakorAuthor Commented:
The reason I ask, is that when I ran it, I was in an interactive mode of some variety.
0
 
ozoCommented:
What is a database mode?
0
 
ozoCommented:
Oops, forgot to turn off debugging when I cut and pasted.
The command should have been.

perl -alne '$x{lc $1}|=$#ARGV if /^(DD\S*)/;END{$x{$_}==0 and print for keys %x}' 1.txt 2.txt
0
 
stakorAuthor Commented:
When I ran the script, I was sitting at a command prompt that read something like DB1 ? I did not write it down.
0
 
stakorAuthor Commented:
I think I figured it out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now