0

I'm trying to find common string in two text files. For example, first text file contains following 1.txt

//documents  #5
##text
//documents/A  #6
//documents/B  #7

second text file contains following: 2.txt

//documents/A  #3

output.txt

//documents/A  #6

If common string found, expected output last number taken from 1.txt file. try.cmd

@echo off
awk "NR==FNR {a[$0]=1;next} !a[$0]" 1.txt 2.txt > output.txt


for /F "usebackq tokens=1" %%A in  ("output.txt") do  (
echo %%A
Mihir
  • 557
  • 1
  • 6
  • 28
  • What is your expected output? – mbomb007 Jun 08 '15 at 20:50
  • output.txt :) update last number from 1.txt if found. – Mihir Jun 08 '15 at 22:42
  • This is a lot to read but the concept should be helpful. http://stackoverflow.com/questions/14324313/batch-file-to-remove-certain-string-from-multiple-files-across-multiple-folders – user4317867 Jun 08 '15 at 22:51
  • If I correctly understand you, you want as result _complete lines from 1.txt_, right? Do you want to search in `1.txt` _the first string_ in each line of `2.txt`? Or search _all strings_ in each line of `2.txt`? – Aacini Jun 09 '15 at 00:09
  • First string in each line of 2.txt and if that string is found in 1.txt than get the second string from 1.txt and replace in 2.txt. Sorry if i made it confusing.. – Mihir Jun 09 '15 at 13:14

1 Answers1

1

Not sure about the requirements, but, maybe

awk "{print $1}" 2.txt | findstr /g:/ /l /b 1.txt

Without awk

cmd /q /c"(for /f "usebackq" %%a in ("2.txt") do echo(%%a)" | findstr /g:/ /l /b 1.txt 

Only awk

awk "{a[$1]=!a[$1]} !a[$1]" 2.txt 1.txt
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • I choose "awk "{a[$1]=!a[$1]} !a[$1]" 2.txt 1.txt" can you please tell me what exactly code doing? and thank you for one line that's exactly what i was looking for.. – Mihir Jun 09 '15 at 13:11
  • @Mihir, without more data I'm not sure the indicated line should be used in production code, please test. It is creating an hash where the key is the first element in the input record and the value is negated for each read. So, the first time the value is readed, the hash is false and as we are storing its negation now it hold a true value. Then the negation of the value is used to determine if the line must be printed or no. Enough for the sample in the question, but .... – MC ND Jun 09 '15 at 15:59