Awk calculation from 2 files piped paste -d
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
The files contain: File1
1 a
2 b
3 c
...
-------------------File2
2 a
2 c
2 d
...
awk receives this format, seperated by comma:
1 a,2 a
2 b,2 c
...
paste -d, File1 File2 | awk -F, 'n=NF/2;s=""; for (i=1;i<=n;i++)printf "%s%s", s, ($i-$(i+n));s=",";; print ""'
I use this line to do a difference row by row of the first field;
First row being 1-2 so prints -1
Ok it's all good I get the right answer, still I don't see how it can pick up the number field when the field is seperated by -F, representing
1 a
If I change
1 a
to
a 1
doing the same with the rest it doesn't work which I agree in logic.
So, in other words, how does it get the number when the field is the number and letter? $i = number, why not letter also?
awk
add a comment |Â
up vote
1
down vote
favorite
The files contain: File1
1 a
2 b
3 c
...
-------------------File2
2 a
2 c
2 d
...
awk receives this format, seperated by comma:
1 a,2 a
2 b,2 c
...
paste -d, File1 File2 | awk -F, 'n=NF/2;s=""; for (i=1;i<=n;i++)printf "%s%s", s, ($i-$(i+n));s=",";; print ""'
I use this line to do a difference row by row of the first field;
First row being 1-2 so prints -1
Ok it's all good I get the right answer, still I don't see how it can pick up the number field when the field is seperated by -F, representing
1 a
If I change
1 a
to
a 1
doing the same with the rest it doesn't work which I agree in logic.
So, in other words, how does it get the number when the field is the number and letter? $i = number, why not letter also?
awk
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
The files contain: File1
1 a
2 b
3 c
...
-------------------File2
2 a
2 c
2 d
...
awk receives this format, seperated by comma:
1 a,2 a
2 b,2 c
...
paste -d, File1 File2 | awk -F, 'n=NF/2;s=""; for (i=1;i<=n;i++)printf "%s%s", s, ($i-$(i+n));s=",";; print ""'
I use this line to do a difference row by row of the first field;
First row being 1-2 so prints -1
Ok it's all good I get the right answer, still I don't see how it can pick up the number field when the field is seperated by -F, representing
1 a
If I change
1 a
to
a 1
doing the same with the rest it doesn't work which I agree in logic.
So, in other words, how does it get the number when the field is the number and letter? $i = number, why not letter also?
awk
The files contain: File1
1 a
2 b
3 c
...
-------------------File2
2 a
2 c
2 d
...
awk receives this format, seperated by comma:
1 a,2 a
2 b,2 c
...
paste -d, File1 File2 | awk -F, 'n=NF/2;s=""; for (i=1;i<=n;i++)printf "%s%s", s, ($i-$(i+n));s=",";; print ""'
I use this line to do a difference row by row of the first field;
First row being 1-2 so prints -1
Ok it's all good I get the right answer, still I don't see how it can pick up the number field when the field is seperated by -F, representing
1 a
If I change
1 a
to
a 1
doing the same with the rest it doesn't work which I agree in logic.
So, in other words, how does it get the number when the field is the number and letter? $i = number, why not letter also?
awk
edited Oct 21 '17 at 11:18
asked Oct 21 '17 at 0:58
Joe
415
415
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
how does it get the number when the field is the number and letter?
When ,
is your field separator (-F,
), text like 2 a
is a single field, and it is simply treated as the number 2 when it is used as a number.
The AWK language is very permissive when you ask it to treat a string as a number. It reads the string until the point where it stops making sense as a number. At that point, if there was something that made sense, that's your number. Otherwise it takes it as 0. This all happens automatically when you use the string in an arithmetic expression. To see how it works, run:
awk 'print $0 + 0'
$0
is the whole line. Adding zero forces it to be interpreted as a number but doesn't change what number that is. Then you can type in lines of input to see how awk
treats them as numbers. You will find, for example, that the empty string is 0, 3a
is 3, 1 2
is 1, 3.4.5.6
is 3.4, and so forth. (Press Ctrl+D at the beginning of a line to signal end-of-input and quit awk
, or just press Ctrl+C.)
Depending on what you actually want to do, you may be able to write a simpler command that is more clear about what it does and how it works, and that can be modified more easily. For example, if each file has just two columns separated by whitespace, and the entries do not themselves contain whitespace, just merge them into a four-column table by running paste
without -d
(or, if you prefer, with something like -d ' '
) and process the table by piping it to awk
without -F
:
paste File1 File2 | awk 'print $1 - $3'
Then it becomes obvious how you would adapt the AWK script when the numbers appear after the letters instead of before:
paste File3 File4 | awk 'print $2 - $4'
(To test this, you can make File3
and File4
from File1
and File2
, respectively, by using awk
to switch the columns. For example, awk 'print $2, $1' File1 > File3
makes File3
.)
Thanks, it does explain why it works. Also, Knowing this I can see how to improve the syntax! It was not a copy/paste the FN was a typing error.
â Joe
Oct 21 '17 at 10:50
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
how does it get the number when the field is the number and letter?
When ,
is your field separator (-F,
), text like 2 a
is a single field, and it is simply treated as the number 2 when it is used as a number.
The AWK language is very permissive when you ask it to treat a string as a number. It reads the string until the point where it stops making sense as a number. At that point, if there was something that made sense, that's your number. Otherwise it takes it as 0. This all happens automatically when you use the string in an arithmetic expression. To see how it works, run:
awk 'print $0 + 0'
$0
is the whole line. Adding zero forces it to be interpreted as a number but doesn't change what number that is. Then you can type in lines of input to see how awk
treats them as numbers. You will find, for example, that the empty string is 0, 3a
is 3, 1 2
is 1, 3.4.5.6
is 3.4, and so forth. (Press Ctrl+D at the beginning of a line to signal end-of-input and quit awk
, or just press Ctrl+C.)
Depending on what you actually want to do, you may be able to write a simpler command that is more clear about what it does and how it works, and that can be modified more easily. For example, if each file has just two columns separated by whitespace, and the entries do not themselves contain whitespace, just merge them into a four-column table by running paste
without -d
(or, if you prefer, with something like -d ' '
) and process the table by piping it to awk
without -F
:
paste File1 File2 | awk 'print $1 - $3'
Then it becomes obvious how you would adapt the AWK script when the numbers appear after the letters instead of before:
paste File3 File4 | awk 'print $2 - $4'
(To test this, you can make File3
and File4
from File1
and File2
, respectively, by using awk
to switch the columns. For example, awk 'print $2, $1' File1 > File3
makes File3
.)
Thanks, it does explain why it works. Also, Knowing this I can see how to improve the syntax! It was not a copy/paste the FN was a typing error.
â Joe
Oct 21 '17 at 10:50
add a comment |Â
up vote
2
down vote
how does it get the number when the field is the number and letter?
When ,
is your field separator (-F,
), text like 2 a
is a single field, and it is simply treated as the number 2 when it is used as a number.
The AWK language is very permissive when you ask it to treat a string as a number. It reads the string until the point where it stops making sense as a number. At that point, if there was something that made sense, that's your number. Otherwise it takes it as 0. This all happens automatically when you use the string in an arithmetic expression. To see how it works, run:
awk 'print $0 + 0'
$0
is the whole line. Adding zero forces it to be interpreted as a number but doesn't change what number that is. Then you can type in lines of input to see how awk
treats them as numbers. You will find, for example, that the empty string is 0, 3a
is 3, 1 2
is 1, 3.4.5.6
is 3.4, and so forth. (Press Ctrl+D at the beginning of a line to signal end-of-input and quit awk
, or just press Ctrl+C.)
Depending on what you actually want to do, you may be able to write a simpler command that is more clear about what it does and how it works, and that can be modified more easily. For example, if each file has just two columns separated by whitespace, and the entries do not themselves contain whitespace, just merge them into a four-column table by running paste
without -d
(or, if you prefer, with something like -d ' '
) and process the table by piping it to awk
without -F
:
paste File1 File2 | awk 'print $1 - $3'
Then it becomes obvious how you would adapt the AWK script when the numbers appear after the letters instead of before:
paste File3 File4 | awk 'print $2 - $4'
(To test this, you can make File3
and File4
from File1
and File2
, respectively, by using awk
to switch the columns. For example, awk 'print $2, $1' File1 > File3
makes File3
.)
Thanks, it does explain why it works. Also, Knowing this I can see how to improve the syntax! It was not a copy/paste the FN was a typing error.
â Joe
Oct 21 '17 at 10:50
add a comment |Â
up vote
2
down vote
up vote
2
down vote
how does it get the number when the field is the number and letter?
When ,
is your field separator (-F,
), text like 2 a
is a single field, and it is simply treated as the number 2 when it is used as a number.
The AWK language is very permissive when you ask it to treat a string as a number. It reads the string until the point where it stops making sense as a number. At that point, if there was something that made sense, that's your number. Otherwise it takes it as 0. This all happens automatically when you use the string in an arithmetic expression. To see how it works, run:
awk 'print $0 + 0'
$0
is the whole line. Adding zero forces it to be interpreted as a number but doesn't change what number that is. Then you can type in lines of input to see how awk
treats them as numbers. You will find, for example, that the empty string is 0, 3a
is 3, 1 2
is 1, 3.4.5.6
is 3.4, and so forth. (Press Ctrl+D at the beginning of a line to signal end-of-input and quit awk
, or just press Ctrl+C.)
Depending on what you actually want to do, you may be able to write a simpler command that is more clear about what it does and how it works, and that can be modified more easily. For example, if each file has just two columns separated by whitespace, and the entries do not themselves contain whitespace, just merge them into a four-column table by running paste
without -d
(or, if you prefer, with something like -d ' '
) and process the table by piping it to awk
without -F
:
paste File1 File2 | awk 'print $1 - $3'
Then it becomes obvious how you would adapt the AWK script when the numbers appear after the letters instead of before:
paste File3 File4 | awk 'print $2 - $4'
(To test this, you can make File3
and File4
from File1
and File2
, respectively, by using awk
to switch the columns. For example, awk 'print $2, $1' File1 > File3
makes File3
.)
how does it get the number when the field is the number and letter?
When ,
is your field separator (-F,
), text like 2 a
is a single field, and it is simply treated as the number 2 when it is used as a number.
The AWK language is very permissive when you ask it to treat a string as a number. It reads the string until the point where it stops making sense as a number. At that point, if there was something that made sense, that's your number. Otherwise it takes it as 0. This all happens automatically when you use the string in an arithmetic expression. To see how it works, run:
awk 'print $0 + 0'
$0
is the whole line. Adding zero forces it to be interpreted as a number but doesn't change what number that is. Then you can type in lines of input to see how awk
treats them as numbers. You will find, for example, that the empty string is 0, 3a
is 3, 1 2
is 1, 3.4.5.6
is 3.4, and so forth. (Press Ctrl+D at the beginning of a line to signal end-of-input and quit awk
, or just press Ctrl+C.)
Depending on what you actually want to do, you may be able to write a simpler command that is more clear about what it does and how it works, and that can be modified more easily. For example, if each file has just two columns separated by whitespace, and the entries do not themselves contain whitespace, just merge them into a four-column table by running paste
without -d
(or, if you prefer, with something like -d ' '
) and process the table by piping it to awk
without -F
:
paste File1 File2 | awk 'print $1 - $3'
Then it becomes obvious how you would adapt the AWK script when the numbers appear after the letters instead of before:
paste File3 File4 | awk 'print $2 - $4'
(To test this, you can make File3
and File4
from File1
and File2
, respectively, by using awk
to switch the columns. For example, awk 'print $2, $1' File1 > File3
makes File3
.)
answered Oct 21 '17 at 2:27
Eliah Kagan
3,16221530
3,16221530
Thanks, it does explain why it works. Also, Knowing this I can see how to improve the syntax! It was not a copy/paste the FN was a typing error.
â Joe
Oct 21 '17 at 10:50
add a comment |Â
Thanks, it does explain why it works. Also, Knowing this I can see how to improve the syntax! It was not a copy/paste the FN was a typing error.
â Joe
Oct 21 '17 at 10:50
Thanks, it does explain why it works. Also, Knowing this I can see how to improve the syntax! It was not a copy/paste the FN was a typing error.
â Joe
Oct 21 '17 at 10:50
Thanks, it does explain why it works. Also, Knowing this I can see how to improve the syntax! It was not a copy/paste the FN was a typing error.
â Joe
Oct 21 '17 at 10:50
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%2f399463%2fawk-calculation-from-2-files-piped-paste-d%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