Find string in one file and use it on another
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
In file 1
I need to find lines that contain string hdu
in column 2, and then in file 2
replace the value in the column 2 with value of column 5 from file 1
.
file1:
64 ha 1 LIG H64 64 0.000000 12.0100 c 0.637086 12.0100
65 du 1 LIG XX65 65 0.000000 16.0000 o -0.547014 16.0000
66 du 1 LIG XX66 66 0.000000 16.0000 oh -0.611114 16.0000
67 hdu 1 LIG HX67 67 0.000000 1.0080 hc 0.090186 1.0080
68 hdu 1 LIG HX68 68 0.000000 1.0080 hc 0.090186 1.0080
file2:
1LIG H64 64 2.06144000 1.94117000 2.65125000
1LIG XX65 65 2.14737000 1.92392000 2.73722000
1LIG XX66 66 1.98154000 2.05095000 2.64407000
1LIG XX67 67 2.21440000 1.72650000 2.55862000
1LIG XX68 68 2.22046000 1.85502000 2.43909000
In the given case, I need to replace XX67
with HX67
, and XX68
with HX68
, so that the output is:
1LIG H64 64 2.06144000 1.94117000 2.65125000
1LIG XX65 65 2.14737000 1.92392000 2.73722000
1LIG XX66 66 1.98154000 2.05095000 2.64407000
1LIG HX67 67 2.21440000 1.72650000 2.55862000
1LIG HX68 68 2.22046000 1.85502000 2.43909000
My best guess was command
awk 'FNR==NR && $2=="hdu" x=$1;y=$5 NR!=FNR if ($3==x) $2=y print' file1 file2
but it replaced only last occurrence.
awk
New contributor
add a comment |Â
up vote
0
down vote
favorite
In file 1
I need to find lines that contain string hdu
in column 2, and then in file 2
replace the value in the column 2 with value of column 5 from file 1
.
file1:
64 ha 1 LIG H64 64 0.000000 12.0100 c 0.637086 12.0100
65 du 1 LIG XX65 65 0.000000 16.0000 o -0.547014 16.0000
66 du 1 LIG XX66 66 0.000000 16.0000 oh -0.611114 16.0000
67 hdu 1 LIG HX67 67 0.000000 1.0080 hc 0.090186 1.0080
68 hdu 1 LIG HX68 68 0.000000 1.0080 hc 0.090186 1.0080
file2:
1LIG H64 64 2.06144000 1.94117000 2.65125000
1LIG XX65 65 2.14737000 1.92392000 2.73722000
1LIG XX66 66 1.98154000 2.05095000 2.64407000
1LIG XX67 67 2.21440000 1.72650000 2.55862000
1LIG XX68 68 2.22046000 1.85502000 2.43909000
In the given case, I need to replace XX67
with HX67
, and XX68
with HX68
, so that the output is:
1LIG H64 64 2.06144000 1.94117000 2.65125000
1LIG XX65 65 2.14737000 1.92392000 2.73722000
1LIG XX66 66 1.98154000 2.05095000 2.64407000
1LIG HX67 67 2.21440000 1.72650000 2.55862000
1LIG HX68 68 2.22046000 1.85502000 2.43909000
My best guess was command
awk 'FNR==NR && $2=="hdu" x=$1;y=$5 NR!=FNR if ($3==x) $2=y print' file1 file2
but it replaced only last occurrence.
awk
New contributor
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In file 1
I need to find lines that contain string hdu
in column 2, and then in file 2
replace the value in the column 2 with value of column 5 from file 1
.
file1:
64 ha 1 LIG H64 64 0.000000 12.0100 c 0.637086 12.0100
65 du 1 LIG XX65 65 0.000000 16.0000 o -0.547014 16.0000
66 du 1 LIG XX66 66 0.000000 16.0000 oh -0.611114 16.0000
67 hdu 1 LIG HX67 67 0.000000 1.0080 hc 0.090186 1.0080
68 hdu 1 LIG HX68 68 0.000000 1.0080 hc 0.090186 1.0080
file2:
1LIG H64 64 2.06144000 1.94117000 2.65125000
1LIG XX65 65 2.14737000 1.92392000 2.73722000
1LIG XX66 66 1.98154000 2.05095000 2.64407000
1LIG XX67 67 2.21440000 1.72650000 2.55862000
1LIG XX68 68 2.22046000 1.85502000 2.43909000
In the given case, I need to replace XX67
with HX67
, and XX68
with HX68
, so that the output is:
1LIG H64 64 2.06144000 1.94117000 2.65125000
1LIG XX65 65 2.14737000 1.92392000 2.73722000
1LIG XX66 66 1.98154000 2.05095000 2.64407000
1LIG HX67 67 2.21440000 1.72650000 2.55862000
1LIG HX68 68 2.22046000 1.85502000 2.43909000
My best guess was command
awk 'FNR==NR && $2=="hdu" x=$1;y=$5 NR!=FNR if ($3==x) $2=y print' file1 file2
but it replaced only last occurrence.
awk
New contributor
In file 1
I need to find lines that contain string hdu
in column 2, and then in file 2
replace the value in the column 2 with value of column 5 from file 1
.
file1:
64 ha 1 LIG H64 64 0.000000 12.0100 c 0.637086 12.0100
65 du 1 LIG XX65 65 0.000000 16.0000 o -0.547014 16.0000
66 du 1 LIG XX66 66 0.000000 16.0000 oh -0.611114 16.0000
67 hdu 1 LIG HX67 67 0.000000 1.0080 hc 0.090186 1.0080
68 hdu 1 LIG HX68 68 0.000000 1.0080 hc 0.090186 1.0080
file2:
1LIG H64 64 2.06144000 1.94117000 2.65125000
1LIG XX65 65 2.14737000 1.92392000 2.73722000
1LIG XX66 66 1.98154000 2.05095000 2.64407000
1LIG XX67 67 2.21440000 1.72650000 2.55862000
1LIG XX68 68 2.22046000 1.85502000 2.43909000
In the given case, I need to replace XX67
with HX67
, and XX68
with HX68
, so that the output is:
1LIG H64 64 2.06144000 1.94117000 2.65125000
1LIG XX65 65 2.14737000 1.92392000 2.73722000
1LIG XX66 66 1.98154000 2.05095000 2.64407000
1LIG HX67 67 2.21440000 1.72650000 2.55862000
1LIG HX68 68 2.22046000 1.85502000 2.43909000
My best guess was command
awk 'FNR==NR && $2=="hdu" x=$1;y=$5 NR!=FNR if ($3==x) $2=y print' file1 file2
but it replaced only last occurrence.
awk
awk
New contributor
New contributor
New contributor
asked 1 min ago
user318543
1
1
New contributor
New contributor
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
user318543 is a new contributor. Be nice, and check out our Code of Conduct.
user318543 is a new contributor. Be nice, and check out our Code of Conduct.
user318543 is a new contributor. Be nice, and check out our Code of Conduct.
user318543 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f478747%2ffind-string-in-one-file-and-use-it-on-another%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