using awk to edit a file based on a second file

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Hi I have a file with a set of number I want to edit a line whenever those numbers show up in the first column of the second file.
a.inp
; atom type fx fy fz
1 1 1000 1000 1000
5 1 1000 1000 1000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 1000 1000 1000
17 1 1000 1000 1000
20 1 1000 1000 1000
...
b.inp
5
15
29
48
72
87
...
output
; atom type fx fy fz
1 1 1000 1000 1000
5 1 100000 100000 100000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 100000 100000 100000
17 1 1000 1000 1000
20 1 1000 1000 1000
...
my code doesn't work for some reason?
awk 'NR == FNR lines[$1]; next
$1 in lines for (col = 3; col <= NF; ++col) $col *= 100 1' b.inp a.inp > new.inp
awk string
add a comment |Â
up vote
0
down vote
favorite
Hi I have a file with a set of number I want to edit a line whenever those numbers show up in the first column of the second file.
a.inp
; atom type fx fy fz
1 1 1000 1000 1000
5 1 1000 1000 1000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 1000 1000 1000
17 1 1000 1000 1000
20 1 1000 1000 1000
...
b.inp
5
15
29
48
72
87
...
output
; atom type fx fy fz
1 1 1000 1000 1000
5 1 100000 100000 100000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 100000 100000 100000
17 1 1000 1000 1000
20 1 1000 1000 1000
...
my code doesn't work for some reason?
awk 'NR == FNR lines[$1]; next
$1 in lines for (col = 3; col <= NF; ++col) $col *= 100 1' b.inp a.inp > new.inp
awk string
Please edit your question and explain how this fails. Telling us simply "it doesn't work", that isn't very helpful. I tried on my machine and it seemed to work fine. What output do you get? How is it wrong? Do you get any output? Any error messages? Also, what operating system are you using? Or, moe importantly, which implementation of awk?
â terdonâ¦
Jul 12 at 18:37
It just outputs the input. doesn't match to do the modification.
â Sarah Aria
Jul 12 at 18:55
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Hi I have a file with a set of number I want to edit a line whenever those numbers show up in the first column of the second file.
a.inp
; atom type fx fy fz
1 1 1000 1000 1000
5 1 1000 1000 1000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 1000 1000 1000
17 1 1000 1000 1000
20 1 1000 1000 1000
...
b.inp
5
15
29
48
72
87
...
output
; atom type fx fy fz
1 1 1000 1000 1000
5 1 100000 100000 100000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 100000 100000 100000
17 1 1000 1000 1000
20 1 1000 1000 1000
...
my code doesn't work for some reason?
awk 'NR == FNR lines[$1]; next
$1 in lines for (col = 3; col <= NF; ++col) $col *= 100 1' b.inp a.inp > new.inp
awk string
Hi I have a file with a set of number I want to edit a line whenever those numbers show up in the first column of the second file.
a.inp
; atom type fx fy fz
1 1 1000 1000 1000
5 1 1000 1000 1000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 1000 1000 1000
17 1 1000 1000 1000
20 1 1000 1000 1000
...
b.inp
5
15
29
48
72
87
...
output
; atom type fx fy fz
1 1 1000 1000 1000
5 1 100000 100000 100000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 100000 100000 100000
17 1 1000 1000 1000
20 1 1000 1000 1000
...
my code doesn't work for some reason?
awk 'NR == FNR lines[$1]; next
$1 in lines for (col = 3; col <= NF; ++col) $col *= 100 1' b.inp a.inp > new.inp
awk string
asked Jul 12 at 18:13
Sarah Aria
31
31
Please edit your question and explain how this fails. Telling us simply "it doesn't work", that isn't very helpful. I tried on my machine and it seemed to work fine. What output do you get? How is it wrong? Do you get any output? Any error messages? Also, what operating system are you using? Or, moe importantly, which implementation of awk?
â terdonâ¦
Jul 12 at 18:37
It just outputs the input. doesn't match to do the modification.
â Sarah Aria
Jul 12 at 18:55
add a comment |Â
Please edit your question and explain how this fails. Telling us simply "it doesn't work", that isn't very helpful. I tried on my machine and it seemed to work fine. What output do you get? How is it wrong? Do you get any output? Any error messages? Also, what operating system are you using? Or, moe importantly, which implementation of awk?
â terdonâ¦
Jul 12 at 18:37
It just outputs the input. doesn't match to do the modification.
â Sarah Aria
Jul 12 at 18:55
Please edit your question and explain how this fails. Telling us simply "it doesn't work", that isn't very helpful. I tried on my machine and it seemed to work fine. What output do you get? How is it wrong? Do you get any output? Any error messages? Also, what operating system are you using? Or, moe importantly, which implementation of awk?
â terdonâ¦
Jul 12 at 18:37
Please edit your question and explain how this fails. Telling us simply "it doesn't work", that isn't very helpful. I tried on my machine and it seemed to work fine. What output do you get? How is it wrong? Do you get any output? Any error messages? Also, what operating system are you using? Or, moe importantly, which implementation of awk?
â terdonâ¦
Jul 12 at 18:37
It just outputs the input. doesn't match to do the modification.
â Sarah Aria
Jul 12 at 18:55
It just outputs the input. doesn't match to do the modification.
â Sarah Aria
Jul 12 at 18:55
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
I can only reproduce what you are seeing if I convert b.inp to a DOS text file.
If you have edited the files with a Windows text editor or produced them on a Windows system, then they may well both be DOS text files.
Convert them to Unix text files with the dos2unix utility.
If b.inp is a DOS text file, the awk code would include the carriage return at the end of the lines in the value of $1 in the first block. These carriage returns would later cause $1 in lines (where $1 comes from a.inp) to never be true.
Converting the files to Unix text files, your code produces
; atom type fx fy fz
1 1 1000 1000 1000
5 1 100000 100000 100000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 100000 100000 100000
17 1 1000 1000 1000
20 1 1000 1000 1000
how can I add a tab to match the spacing?
â Sarah Aria
Jul 12 at 19:22
@SarahAria Is the original file using tabs between all columns?
â Kusalananda
Jul 12 at 19:24
yes, I just want to add a tab before the rows i edit. that should be enough.
â Sarah Aria
Jul 12 at 19:31
@SarahAria You may do that with$0 = "t" $0at the very end of the second block (with a;in-between$col *= 100and that).
â Kusalananda
Jul 12 at 19:35
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I can only reproduce what you are seeing if I convert b.inp to a DOS text file.
If you have edited the files with a Windows text editor or produced them on a Windows system, then they may well both be DOS text files.
Convert them to Unix text files with the dos2unix utility.
If b.inp is a DOS text file, the awk code would include the carriage return at the end of the lines in the value of $1 in the first block. These carriage returns would later cause $1 in lines (where $1 comes from a.inp) to never be true.
Converting the files to Unix text files, your code produces
; atom type fx fy fz
1 1 1000 1000 1000
5 1 100000 100000 100000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 100000 100000 100000
17 1 1000 1000 1000
20 1 1000 1000 1000
how can I add a tab to match the spacing?
â Sarah Aria
Jul 12 at 19:22
@SarahAria Is the original file using tabs between all columns?
â Kusalananda
Jul 12 at 19:24
yes, I just want to add a tab before the rows i edit. that should be enough.
â Sarah Aria
Jul 12 at 19:31
@SarahAria You may do that with$0 = "t" $0at the very end of the second block (with a;in-between$col *= 100and that).
â Kusalananda
Jul 12 at 19:35
add a comment |Â
up vote
2
down vote
accepted
I can only reproduce what you are seeing if I convert b.inp to a DOS text file.
If you have edited the files with a Windows text editor or produced them on a Windows system, then they may well both be DOS text files.
Convert them to Unix text files with the dos2unix utility.
If b.inp is a DOS text file, the awk code would include the carriage return at the end of the lines in the value of $1 in the first block. These carriage returns would later cause $1 in lines (where $1 comes from a.inp) to never be true.
Converting the files to Unix text files, your code produces
; atom type fx fy fz
1 1 1000 1000 1000
5 1 100000 100000 100000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 100000 100000 100000
17 1 1000 1000 1000
20 1 1000 1000 1000
how can I add a tab to match the spacing?
â Sarah Aria
Jul 12 at 19:22
@SarahAria Is the original file using tabs between all columns?
â Kusalananda
Jul 12 at 19:24
yes, I just want to add a tab before the rows i edit. that should be enough.
â Sarah Aria
Jul 12 at 19:31
@SarahAria You may do that with$0 = "t" $0at the very end of the second block (with a;in-between$col *= 100and that).
â Kusalananda
Jul 12 at 19:35
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I can only reproduce what you are seeing if I convert b.inp to a DOS text file.
If you have edited the files with a Windows text editor or produced them on a Windows system, then they may well both be DOS text files.
Convert them to Unix text files with the dos2unix utility.
If b.inp is a DOS text file, the awk code would include the carriage return at the end of the lines in the value of $1 in the first block. These carriage returns would later cause $1 in lines (where $1 comes from a.inp) to never be true.
Converting the files to Unix text files, your code produces
; atom type fx fy fz
1 1 1000 1000 1000
5 1 100000 100000 100000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 100000 100000 100000
17 1 1000 1000 1000
20 1 1000 1000 1000
I can only reproduce what you are seeing if I convert b.inp to a DOS text file.
If you have edited the files with a Windows text editor or produced them on a Windows system, then they may well both be DOS text files.
Convert them to Unix text files with the dos2unix utility.
If b.inp is a DOS text file, the awk code would include the carriage return at the end of the lines in the value of $1 in the first block. These carriage returns would later cause $1 in lines (where $1 comes from a.inp) to never be true.
Converting the files to Unix text files, your code produces
; atom type fx fy fz
1 1 1000 1000 1000
5 1 100000 100000 100000
7 1 1000 1000 1000
11 1 1000 1000 1000
12 1 1000 1000 1000
13 1 1000 1000 1000
15 1 100000 100000 100000
17 1 1000 1000 1000
20 1 1000 1000 1000
edited Jul 12 at 19:18
answered Jul 12 at 19:13
Kusalananda
101k13199312
101k13199312
how can I add a tab to match the spacing?
â Sarah Aria
Jul 12 at 19:22
@SarahAria Is the original file using tabs between all columns?
â Kusalananda
Jul 12 at 19:24
yes, I just want to add a tab before the rows i edit. that should be enough.
â Sarah Aria
Jul 12 at 19:31
@SarahAria You may do that with$0 = "t" $0at the very end of the second block (with a;in-between$col *= 100and that).
â Kusalananda
Jul 12 at 19:35
add a comment |Â
how can I add a tab to match the spacing?
â Sarah Aria
Jul 12 at 19:22
@SarahAria Is the original file using tabs between all columns?
â Kusalananda
Jul 12 at 19:24
yes, I just want to add a tab before the rows i edit. that should be enough.
â Sarah Aria
Jul 12 at 19:31
@SarahAria You may do that with$0 = "t" $0at the very end of the second block (with a;in-between$col *= 100and that).
â Kusalananda
Jul 12 at 19:35
how can I add a tab to match the spacing?
â Sarah Aria
Jul 12 at 19:22
how can I add a tab to match the spacing?
â Sarah Aria
Jul 12 at 19:22
@SarahAria Is the original file using tabs between all columns?
â Kusalananda
Jul 12 at 19:24
@SarahAria Is the original file using tabs between all columns?
â Kusalananda
Jul 12 at 19:24
yes, I just want to add a tab before the rows i edit. that should be enough.
â Sarah Aria
Jul 12 at 19:31
yes, I just want to add a tab before the rows i edit. that should be enough.
â Sarah Aria
Jul 12 at 19:31
@SarahAria You may do that with
$0 = "t" $0 at the very end of the second block (with a ; in-between $col *= 100 and that).â Kusalananda
Jul 12 at 19:35
@SarahAria You may do that with
$0 = "t" $0 at the very end of the second block (with a ; in-between $col *= 100 and that).â Kusalananda
Jul 12 at 19:35
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%2f454952%2fusing-awk-to-edit-a-file-based-on-a-second-file%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
Please edit your question and explain how this fails. Telling us simply "it doesn't work", that isn't very helpful. I tried on my machine and it seemed to work fine. What output do you get? How is it wrong? Do you get any output? Any error messages? Also, what operating system are you using? Or, moe importantly, which implementation of awk?
â terdonâ¦
Jul 12 at 18:37
It just outputs the input. doesn't match to do the modification.
â Sarah Aria
Jul 12 at 18:55