I want a bash script that will take Item 1 in File1, traverse all lines in File2 and output if a match exists. Continuing the pattern recursively, Item2, File1, traverse all lines in File2 for a match, continue this pattern until all lines in File one have been processed.
Now, check this out, sample data.
File1 - single column of hostnames, using the short name
vsie1p990
vsie1p991
vsie1p992
...
File2 - multi-column, comma separated, the first column is the hostname(shortname)
format: shortname, IP Address, fqdn
> vsie1p992,191.167.44.212,vsie1p992.srv.us.company.com
I tried the following, but something is just not quite right:
Example run: > ./checkit.sh list1 list2
Any help, advice, guidance would be greatly appreciated.
Will a given hostname ever appear on more than one line in list2?
Can a hostname in list1 ever be a substring of another hostname in list1 or list2?
Does the output have to be in the order of hostnames in list1? Is it ok if the output is in the order of appearance of where those hostnames appear in list2?
Assuming that hostnames in file1 are distinct and none of them are a subset of any other hostname, the command:
will give you all lines in file2 that contain any of the hostnames listed in file1 using a case insensitive match.
So, in your code:
where is the closing } and where is the closing done? If you're creating a subshell, variables defined in the subshell won't be available in the parent.
Ok, so here is the deal, I have 179 entries in file1 and I have 118875 entries in file2.
If I do this:
, it returns a match
The point is, all 179 entries "do" exist in file2, so I simply (ugh :-)) want
to export the hostname,ipaddress,fqdn for each match from file2
Here is the complete code (not working yet):
The irony is, code similar to this has worked fine for me in the past with (or using) other data sets. Obviously my "lack of understanding" is now exhibited because the code is not behaving as I would expect it would. Thus my confusion.
Last edited by Scrutinizer; 06-29-2014 at 12:53 PM..
Reason: CODE tags
Ok, so here is the deal, I have 179 entries in file1 and I have 118875 entries in file2.
If I do this:
, it returns a match
The point is, all 179 entries "do" exist in file2, so I simply (ugh :-)) want
to export the hostname,ipaddress,fqdn for each match from file2
Here is the complete code (not working yet):
The irony is, code similar to this has worked fine for me in the past with (or using) other data sets. Obviously my "lack of understanding" is now exhibited because the code is not behaving as I would expect it would. Thus my confusion.
I'm surprised that bash ran this script without complaining about the missing " in the line:
Your code would be easier to read if you would use indentation to make the structure obvious.
The braces ({ and }) in your code are unnecessary, but shouldn't affect the behavior of this script.
You could simplify some of your code by redirecting the output of the while loop instead of redirecting the output of each command in the loop, but that shouldn't affect the output produced by this script either.
What is the name of your script? What are the permissions on your script? How did you invoke your script?
If every line in file1 matches a line in file2 when using the command:
and you add the missing quote in:
and your script is executable, and you invoke it with something like the command:
it seems like you should get something in outfile as long as file1 and file2 do not contain any whitespace characters. (If they do contain whitespace characters and you quoted them appropriately when you invoked your script, your script is still missing quotes around uses of "$1" and "$2" (but, again, if that is the problem, bash should have given you syntax errors).
What does "this value is blank" mean? Is there no output in outfile from the echo? Is there output from each echo, but it is just IT = ? What is in outfile when your script completes?
Please explain how this script is behaving differently than you expect.
Please explain what you expect this script to do.
Please show us what the script is doing?
Please answer the questions I asked in post #2 in this thread.
Does the grep command I suggested in post #4 in this thread produce any output? If so, is it suitable for the main body of your report?
How did you create your files? Using a windows text editor? Then no surprise there's no matches due to the trailing CR char (excerpt from a script run with -vx set):
You can find out with e.g.
, or hexdump / od.
In the tab-delimited files, I am trying to match
$1,$2,$3,$4,$5 in fiel1 with $1,$2,$3,$4,$5 in fiel2 and create and output file that lists what matches and what was not found (or doesn't match).
However the awk below seems to skip the first line and does not produce the desired output. I think... (2 Replies)
I am trying to output the matches between $1 of file1 to $3 of file2 into a new file match.
I am also wanting to output the mismatches between those same 2 files and fields to two separate new files called missing from file1 and missing from file2. The input files are tab-delimited, but the... (9 Replies)
Hi all,
I have two files, chap.txt and complex.txt.
chap.txt looks like this:
a
d
l
m
r
k
complex.txt looks like this:
a c d e l m n j
a d l p q r
c p r m
......... (7 Replies)
Hi,
I am looking for a way to compare two text files and print the matches. For example;
File1.txt
89473036
78474384
48948408
95754748
47849030
File2.txt
47849030
46730356
16734947
78474384
36340047
Output: (11 Replies)
Hi,
I have two files, file1 and file2 and I need to compare them by line (exact match, order of the lines is not important) and get output with lines from file2 that are not found in file1 (not other way around).
How do I do that? With grep or otherwise..
Thankyou (2 Replies)
I have two arrays I need to compare against a third, looking for matches, not differences. I think I'm going to have to convert the arrays to files and grep them, but I'm not too sure if there's a tool to enable me to matches specifically, instead of differences.
Thanks in advance!
Karl (9 Replies)
I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together.
I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Hi,
I have a question of comparing to files and output
the result third file where file1 is the mainfile containing processed dir data
and 2nd file grepīs dirīs data again (could be newer dirs comparing file1<file2)
now i wanna make shure that output in file3 only contains newer dirs
hx... (1 Reply)
Hi,
In File1.txt I have text like:
23AA3424DD23|3423ff25sd5345| and so on
In File2.txt I have similar text as File1, but with ",":
23aa3424dd23,192.168.1.100, and so on
I wan to remove the pipes from File1 and select 5 fields, then remove "," from File2.txt and select 2 fields (IP's... (14 Replies)
I have two files which are of the following format:
File1:
Unnamed
Unnamed
Boston
Unnamed
New_York
Unnamed
File2:
San_Francisco
Chicago
Portland
Austin
Orlando
Los_Angeles
In the case where an entry in File1 is "Unnamed", I want to output the name from the same line in File2.... (5 Replies)