Compare two files and modify input upon match
Clash 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
text-processing
add a comment |Â
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
text-processing
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
add a comment |Â
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
text-processing
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
text-processing
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
add a comment |Â
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
add a comment |Â
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 namedseen
, then read next linenext
(actually goto first and run this block again untilNR!=FNR
)($2 in seen)print "// "$0; next1
this is only apply for second input file (here file1), and looking for theseen
array if contains same string as column#2$2
in file1, then print entire line of file1 with pre-appended//
, and gotonext
check condition again until it's match, otherwise print the entire line with1
condition (that's enableawk
's default action).
1
@ñÃÂsýù÷: Thanks a lot, it's very clear
â Khanh Nguyen
Sep 26 '17 at 6:35
add a comment |Â
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.
Thanks a lot Philippos, and get multi choice with your script and detail information
â Khanh Nguyen
Sep 26 '17 at 6:38
add a comment |Â
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 namedseen
, then read next linenext
(actually goto first and run this block again untilNR!=FNR
)($2 in seen)print "// "$0; next1
this is only apply for second input file (here file1), and looking for theseen
array if contains same string as column#2$2
in file1, then print entire line of file1 with pre-appended//
, and gotonext
check condition again until it's match, otherwise print the entire line with1
condition (that's enableawk
's default action).
1
@ñÃÂsýù÷: Thanks a lot, it's very clear
â Khanh Nguyen
Sep 26 '17 at 6:35
add a comment |Â
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 namedseen
, then read next linenext
(actually goto first and run this block again untilNR!=FNR
)($2 in seen)print "// "$0; next1
this is only apply for second input file (here file1), and looking for theseen
array if contains same string as column#2$2
in file1, then print entire line of file1 with pre-appended//
, and gotonext
check condition again until it's match, otherwise print the entire line with1
condition (that's enableawk
's default action).
1
@ñÃÂsýù÷: Thanks a lot, it's very clear
â Khanh Nguyen
Sep 26 '17 at 6:35
add a comment |Â
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 namedseen
, then read next linenext
(actually goto first and run this block again untilNR!=FNR
)($2 in seen)print "// "$0; next1
this is only apply for second input file (here file1), and looking for theseen
array if contains same string as column#2$2
in file1, then print entire line of file1 with pre-appended//
, and gotonext
check condition again until it's match, otherwise print the entire line with1
condition (that's enableawk
's default action).
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 namedseen
, then read next linenext
(actually goto first and run this block again untilNR!=FNR
)($2 in seen)print "// "$0; next1
this is only apply for second input file (here file1), and looking for theseen
array if contains same string as column#2$2
in file1, then print entire line of file1 with pre-appended//
, and gotonext
check condition again until it's match, otherwise print the entire line with1
condition (that's enableawk
's default action).
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
add a comment |Â
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
add a comment |Â
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.
Thanks a lot Philippos, and get multi choice with your script and detail information
â Khanh Nguyen
Sep 26 '17 at 6:38
add a comment |Â
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.
Thanks a lot Philippos, and get multi choice with your script and detail information
â Khanh Nguyen
Sep 26 '17 at 6:38
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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