Compare two files and modify input upon match

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I want to compare file1 with file2.



file1 contains a set of pathnames and file2 contains a set of words.



If any pathname contains any of the words in the second file, it should be commented out by prepending the line with //.



file1:



xxx/AAA.tmp.v
xxx/BBB.tmp.v
xxx/CCC.tmp.v


file2:



BBB
CCC
FFF


Desired output in a new file:



xxx/AAA.tmp.v
// xxx/BBB.tmp.v
// xxx/CCC.tmp.v









share|improve this question























  • is the data to be matched from file2 always in the form of /data. in file1? solution would be easier if that is the case... and it is better to add your efforts made and point out where you got stuck...
    – Sundeep
    Sep 26 '17 at 4:50










  • @Sundeep: your script no error syntax and result exactly same my expectation, thanks a lot. awk 'NR==FNRa[$0]; next for(line in a)if($0 ~ line)$0="// "$0 1' file2 file1
    – Khanh Nguyen
    Sep 26 '17 at 6:37














up vote
0
down vote

favorite












I want to compare file1 with file2.



file1 contains a set of pathnames and file2 contains a set of words.



If any pathname contains any of the words in the second file, it should be commented out by prepending the line with //.



file1:



xxx/AAA.tmp.v
xxx/BBB.tmp.v
xxx/CCC.tmp.v


file2:



BBB
CCC
FFF


Desired output in a new file:



xxx/AAA.tmp.v
// xxx/BBB.tmp.v
// xxx/CCC.tmp.v









share|improve this question























  • is the data to be matched from file2 always in the form of /data. in file1? solution would be easier if that is the case... and it is better to add your efforts made and point out where you got stuck...
    – Sundeep
    Sep 26 '17 at 4:50










  • @Sundeep: your script no error syntax and result exactly same my expectation, thanks a lot. awk 'NR==FNRa[$0]; next for(line in a)if($0 ~ line)$0="// "$0 1' file2 file1
    – Khanh Nguyen
    Sep 26 '17 at 6:37












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to compare file1 with file2.



file1 contains a set of pathnames and file2 contains a set of words.



If any pathname contains any of the words in the second file, it should be commented out by prepending the line with //.



file1:



xxx/AAA.tmp.v
xxx/BBB.tmp.v
xxx/CCC.tmp.v


file2:



BBB
CCC
FFF


Desired output in a new file:



xxx/AAA.tmp.v
// xxx/BBB.tmp.v
// xxx/CCC.tmp.v









share|improve this question















I want to compare file1 with file2.



file1 contains a set of pathnames and file2 contains a set of words.



If any pathname contains any of the words in the second file, it should be commented out by prepending the line with //.



file1:



xxx/AAA.tmp.v
xxx/BBB.tmp.v
xxx/CCC.tmp.v


file2:



BBB
CCC
FFF


Desired output in a new file:



xxx/AAA.tmp.v
// xxx/BBB.tmp.v
// xxx/CCC.tmp.v






text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 27 '17 at 10:09









Kusalananda

106k14209327




106k14209327










asked Sep 26 '17 at 3:56









Khanh Nguyen

32




32











  • is the data to be matched from file2 always in the form of /data. in file1? solution would be easier if that is the case... and it is better to add your efforts made and point out where you got stuck...
    – Sundeep
    Sep 26 '17 at 4:50










  • @Sundeep: your script no error syntax and result exactly same my expectation, thanks a lot. awk 'NR==FNRa[$0]; next for(line in a)if($0 ~ line)$0="// "$0 1' file2 file1
    – Khanh Nguyen
    Sep 26 '17 at 6:37
















  • is the data to be matched from file2 always in the form of /data. in file1? solution would be easier if that is the case... and it is better to add your efforts made and point out where you got stuck...
    – Sundeep
    Sep 26 '17 at 4:50










  • @Sundeep: your script no error syntax and result exactly same my expectation, thanks a lot. awk 'NR==FNRa[$0]; next for(line in a)if($0 ~ line)$0="// "$0 1' file2 file1
    – Khanh Nguyen
    Sep 26 '17 at 6:37















is the data to be matched from file2 always in the form of /data. in file1? solution would be easier if that is the case... and it is better to add your efforts made and point out where you got stuck...
– Sundeep
Sep 26 '17 at 4:50




is the data to be matched from file2 always in the form of /data. in file1? solution would be easier if that is the case... and it is better to add your efforts made and point out where you got stuck...
– Sundeep
Sep 26 '17 at 4:50












@Sundeep: your script no error syntax and result exactly same my expectation, thanks a lot. awk 'NR==FNRa[$0]; next for(line in a)if($0 ~ line)$0="// "$0 1' file2 file1
– Khanh Nguyen
Sep 26 '17 at 6:37




@Sundeep: your script no error syntax and result exactly same my expectation, thanks a lot. awk 'NR==FNRa[$0]; next for(line in a)if($0 ~ line)$0="// "$0 1' file2 file1
– Khanh Nguyen
Sep 26 '17 at 6:37










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You could use awk.



awk -F"[/.]" 'NR==FNRseen[$0];next 
($2 in seen)print "// "$0; next1
' file2 file1



  • -F"[/.]" defines fields delimiters as slash / or point ..


  • NR==FNR this is true always for first input data (here file2), Record Number==File Record Number.


  • seen[$0];next if above is true, then hold entire line of file2 into array named seen, then read next line next (actually goto first and run this block again until NR!=FNR)


  • ($2 in seen)print "// "$0; next1 this is only apply for second input file (here file1), and looking for the seen array if contains same string as column#2 $2 in file1, then print entire line of file1 with pre-appended //, and goto next check condition again until it's match, otherwise print the entire line with 1 condition (that's enable awk's default action).





share|improve this answer


















  • 1




    @αғsнιη: Thanks a lot, it's very clear
    – Khanh Nguyen
    Sep 26 '17 at 6:35

















up vote
0
down vote













With sed you can do it like this:



sed '/tmp/!H;d;
G;s/$/
/;s_.*(..*).*n1n_// &_
P;d' file2 file1


You collect the patterns of file2 in the hold space and for each line of file1 you append that collection and check with backreferences whether the pattern is found in the line.



For a more detailed explanation see this question and answer.



Note that I used the string tmp as indication that we are in file1; you may need to adapt that to your actual case. The strange substitution starting in line 2 adds a newline to the end so we know that each pattern will be surrounded by newlines.






share|improve this answer






















  • Thanks a lot Philippos, and get multi choice with your script and detail information
    – Khanh Nguyen
    Sep 26 '17 at 6:38










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394450%2fcompare-two-files-and-modify-input-upon-match%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You could use awk.



awk -F"[/.]" 'NR==FNRseen[$0];next 
($2 in seen)print "// "$0; next1
' file2 file1



  • -F"[/.]" defines fields delimiters as slash / or point ..


  • NR==FNR this is true always for first input data (here file2), Record Number==File Record Number.


  • seen[$0];next if above is true, then hold entire line of file2 into array named seen, then read next line next (actually goto first and run this block again until NR!=FNR)


  • ($2 in seen)print "// "$0; next1 this is only apply for second input file (here file1), and looking for the seen array if contains same string as column#2 $2 in file1, then print entire line of file1 with pre-appended //, and goto next check condition again until it's match, otherwise print the entire line with 1 condition (that's enable awk's default action).





share|improve this answer


















  • 1




    @αғsнιη: Thanks a lot, it's very clear
    – Khanh Nguyen
    Sep 26 '17 at 6:35














up vote
1
down vote



accepted










You could use awk.



awk -F"[/.]" 'NR==FNRseen[$0];next 
($2 in seen)print "// "$0; next1
' file2 file1



  • -F"[/.]" defines fields delimiters as slash / or point ..


  • NR==FNR this is true always for first input data (here file2), Record Number==File Record Number.


  • seen[$0];next if above is true, then hold entire line of file2 into array named seen, then read next line next (actually goto first and run this block again until NR!=FNR)


  • ($2 in seen)print "// "$0; next1 this is only apply for second input file (here file1), and looking for the seen array if contains same string as column#2 $2 in file1, then print entire line of file1 with pre-appended //, and goto next check condition again until it's match, otherwise print the entire line with 1 condition (that's enable awk's default action).





share|improve this answer


















  • 1




    @αғsнιη: Thanks a lot, it's very clear
    – Khanh Nguyen
    Sep 26 '17 at 6:35












up vote
1
down vote



accepted







up vote
1
down vote



accepted






You could use awk.



awk -F"[/.]" 'NR==FNRseen[$0];next 
($2 in seen)print "// "$0; next1
' file2 file1



  • -F"[/.]" defines fields delimiters as slash / or point ..


  • NR==FNR this is true always for first input data (here file2), Record Number==File Record Number.


  • seen[$0];next if above is true, then hold entire line of file2 into array named seen, then read next line next (actually goto first and run this block again until NR!=FNR)


  • ($2 in seen)print "// "$0; next1 this is only apply for second input file (here file1), and looking for the seen array if contains same string as column#2 $2 in file1, then print entire line of file1 with pre-appended //, and goto next check condition again until it's match, otherwise print the entire line with 1 condition (that's enable awk's default action).





share|improve this answer














You could use awk.



awk -F"[/.]" 'NR==FNRseen[$0];next 
($2 in seen)print "// "$0; next1
' file2 file1



  • -F"[/.]" defines fields delimiters as slash / or point ..


  • NR==FNR this is true always for first input data (here file2), Record Number==File Record Number.


  • seen[$0];next if above is true, then hold entire line of file2 into array named seen, then read next line next (actually goto first and run this block again until NR!=FNR)


  • ($2 in seen)print "// "$0; next1 this is only apply for second input file (here file1), and looking for the seen array if contains same string as column#2 $2 in file1, then print entire line of file1 with pre-appended //, and goto next check condition again until it's match, otherwise print the entire line with 1 condition (that's enable awk's default action).






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 26 '17 at 5:08

























answered Sep 26 '17 at 4:03









αғsнιη

15.7k92563




15.7k92563







  • 1




    @αғsнιη: Thanks a lot, it's very clear
    – Khanh Nguyen
    Sep 26 '17 at 6:35












  • 1




    @αғsнιη: Thanks a lot, it's very clear
    – Khanh Nguyen
    Sep 26 '17 at 6:35







1




1




@αғsнιη: Thanks a lot, it's very clear
– Khanh Nguyen
Sep 26 '17 at 6:35




@αғsнιη: Thanks a lot, it's very clear
– Khanh Nguyen
Sep 26 '17 at 6:35












up vote
0
down vote













With sed you can do it like this:



sed '/tmp/!H;d;
G;s/$/
/;s_.*(..*).*n1n_// &_
P;d' file2 file1


You collect the patterns of file2 in the hold space and for each line of file1 you append that collection and check with backreferences whether the pattern is found in the line.



For a more detailed explanation see this question and answer.



Note that I used the string tmp as indication that we are in file1; you may need to adapt that to your actual case. The strange substitution starting in line 2 adds a newline to the end so we know that each pattern will be surrounded by newlines.






share|improve this answer






















  • Thanks a lot Philippos, and get multi choice with your script and detail information
    – Khanh Nguyen
    Sep 26 '17 at 6:38














up vote
0
down vote













With sed you can do it like this:



sed '/tmp/!H;d;
G;s/$/
/;s_.*(..*).*n1n_// &_
P;d' file2 file1


You collect the patterns of file2 in the hold space and for each line of file1 you append that collection and check with backreferences whether the pattern is found in the line.



For a more detailed explanation see this question and answer.



Note that I used the string tmp as indication that we are in file1; you may need to adapt that to your actual case. The strange substitution starting in line 2 adds a newline to the end so we know that each pattern will be surrounded by newlines.






share|improve this answer






















  • Thanks a lot Philippos, and get multi choice with your script and detail information
    – Khanh Nguyen
    Sep 26 '17 at 6:38












up vote
0
down vote










up vote
0
down vote









With sed you can do it like this:



sed '/tmp/!H;d;
G;s/$/
/;s_.*(..*).*n1n_// &_
P;d' file2 file1


You collect the patterns of file2 in the hold space and for each line of file1 you append that collection and check with backreferences whether the pattern is found in the line.



For a more detailed explanation see this question and answer.



Note that I used the string tmp as indication that we are in file1; you may need to adapt that to your actual case. The strange substitution starting in line 2 adds a newline to the end so we know that each pattern will be surrounded by newlines.






share|improve this answer














With sed you can do it like this:



sed '/tmp/!H;d;
G;s/$/
/;s_.*(..*).*n1n_// &_
P;d' file2 file1


You collect the patterns of file2 in the hold space and for each line of file1 you append that collection and check with backreferences whether the pattern is found in the line.



For a more detailed explanation see this question and answer.



Note that I used the string tmp as indication that we are in file1; you may need to adapt that to your actual case. The strange substitution starting in line 2 adds a newline to the end so we know that each pattern will be surrounded by newlines.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 26 '17 at 6:03

























answered Sep 26 '17 at 5:51









Philippos

5,95211546




5,95211546











  • Thanks a lot Philippos, and get multi choice with your script and detail information
    – Khanh Nguyen
    Sep 26 '17 at 6:38
















  • Thanks a lot Philippos, and get multi choice with your script and detail information
    – Khanh Nguyen
    Sep 26 '17 at 6:38















Thanks a lot Philippos, and get multi choice with your script and detail information
– Khanh Nguyen
Sep 26 '17 at 6:38




Thanks a lot Philippos, and get multi choice with your script and detail information
– Khanh Nguyen
Sep 26 '17 at 6:38

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394450%2fcompare-two-files-and-modify-input-upon-match%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?