Hi,
I have two files file1.txt and file2.txt. Please see the attachments.
In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to search backward for the path and print the corresponding path (/home/saurabh/file1.txt). Please note that there may be many entries between the path(/home/saurabh/file1.txt) and the line containing the pattern 1172c1172.
These solutions seems to work for most of the test-cases. Haven't had much experience with awk. Please let me know ,if I understand correctly these solutions
Raja,
In the first line you extracted the pattern from file2 and stored them in a variable var. Then in the second line for any line starting with / the whole line($0) is stored in a and then you perform a match with pat($0~pat) and print a .Can you please explain how does matching $0 with pat works because $0 will contain the path and pat will contain the pattern(1172c1172).
RudyC,
Can you please explain your solution .Will this solution work if file2 has more than one entry
That solution keeps the second field in the second line of the second file as the search pattern. Then it reads file1 line by line; if it finds a PATH, it keeps it; if it finds the search pattern, it prints PATH, which is the last one found.
Neither of the solutions will handle two or more search patterns. You didn't specify that. How can search patterns be identified in file2? Is it always < ....c.... ? Or what patterns do you have in mind now?
$0 ~ pat will be true only when awk approaches the line containing 1172c1172. Thats when the script prints the previously saved path string (variable 'a') and exit. I hope you can get this.