using awk to edit a file based on a second file

The name of the pictureThe name of the pictureThe name of the pictureClash 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






share|improve this question



















  • 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














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






share|improve this question



















  • 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












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






share|improve this question











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








share|improve this question










share|improve this question




share|improve this question









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
















  • 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










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





share|improve this answer























  • 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" $0 at the very end of the second block (with a ; in-between $col *= 100 and that).
    – Kusalananda
    Jul 12 at 19:35











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%2f454952%2fusing-awk-to-edit-a-file-based-on-a-second-file%23new-answer', 'question_page');

);

Post as a guest






























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





share|improve this answer























  • 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" $0 at the very end of the second block (with a ; in-between $col *= 100 and that).
    – Kusalananda
    Jul 12 at 19:35















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





share|improve this answer























  • 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" $0 at the very end of the second block (with a ; in-between $col *= 100 and that).
    – Kusalananda
    Jul 12 at 19:35













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





share|improve this answer















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






share|improve this answer















share|improve this answer



share|improve this answer








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" $0 at the very end of the second block (with a ; in-between $col *= 100 and that).
    – Kusalananda
    Jul 12 at 19:35

















  • 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" $0 at the very end of the second block (with a ; in-between $col *= 100 and 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













 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos