Printing the difference between adjacent values in a column to a new column
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a file
A 1
A 2
A 4
A 6
I want to print the difference between adjacent values (below-above) in column 2 to a new column 3, to get this
A 1
A 2 1
A 4 2
A 6 2
I have discovered something like this on SO, but failed to print it as a new column.
awk 'NR>1print $1-p p=$1' file
awk
add a comment |Â
up vote
0
down vote
favorite
I have a file
A 1
A 2
A 4
A 6
I want to print the difference between adjacent values (below-above) in column 2 to a new column 3, to get this
A 1
A 2 1
A 4 2
A 6 2
I have discovered something like this on SO, but failed to print it as a new column.
awk 'NR>1print $1-p p=$1' file
awk
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file
A 1
A 2
A 4
A 6
I want to print the difference between adjacent values (below-above) in column 2 to a new column 3, to get this
A 1
A 2 1
A 4 2
A 6 2
I have discovered something like this on SO, but failed to print it as a new column.
awk 'NR>1print $1-p p=$1' file
awk
I have a file
A 1
A 2
A 4
A 6
I want to print the difference between adjacent values (below-above) in column 2 to a new column 3, to get this
A 1
A 2 1
A 4 2
A 6 2
I have discovered something like this on SO, but failed to print it as a new column.
awk 'NR>1print $1-p p=$1' file
awk
awk
edited Oct 3 '17 at 3:59
ñÃÂsýù÷
15.7k92563
15.7k92563
asked Oct 3 '17 at 3:29
Johnny Tam
1208
1208
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
To modify the given code in question
$ awk 'NR>1$3=$2-p p=$2 1' file
A 1
A 2 1
A 4 2
A 6 2
- Fields are indexed from
1
, so use$2
for second column$0
contains entire input record
- After modifying, you need to print the record. Default action is printing contents of
$0
if condition is true.1
is used idiomatically for such cases
Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
â Johnny Tam
Oct 4 '17 at 4:14
@JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need?awk 'NR>1$3=int($2/p) p=$2 1'
â Sundeep
Oct 4 '17 at 4:58
Please go here: unix.stackexchange.com/questions/396195/⦠for a simplified illustration. :)
â Johnny Tam
Oct 5 '17 at 6:13
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
To modify the given code in question
$ awk 'NR>1$3=$2-p p=$2 1' file
A 1
A 2 1
A 4 2
A 6 2
- Fields are indexed from
1
, so use$2
for second column$0
contains entire input record
- After modifying, you need to print the record. Default action is printing contents of
$0
if condition is true.1
is used idiomatically for such cases
Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
â Johnny Tam
Oct 4 '17 at 4:14
@JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need?awk 'NR>1$3=int($2/p) p=$2 1'
â Sundeep
Oct 4 '17 at 4:58
Please go here: unix.stackexchange.com/questions/396195/⦠for a simplified illustration. :)
â Johnny Tam
Oct 5 '17 at 6:13
add a comment |Â
up vote
5
down vote
accepted
To modify the given code in question
$ awk 'NR>1$3=$2-p p=$2 1' file
A 1
A 2 1
A 4 2
A 6 2
- Fields are indexed from
1
, so use$2
for second column$0
contains entire input record
- After modifying, you need to print the record. Default action is printing contents of
$0
if condition is true.1
is used idiomatically for such cases
Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
â Johnny Tam
Oct 4 '17 at 4:14
@JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need?awk 'NR>1$3=int($2/p) p=$2 1'
â Sundeep
Oct 4 '17 at 4:58
Please go here: unix.stackexchange.com/questions/396195/⦠for a simplified illustration. :)
â Johnny Tam
Oct 5 '17 at 6:13
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
To modify the given code in question
$ awk 'NR>1$3=$2-p p=$2 1' file
A 1
A 2 1
A 4 2
A 6 2
- Fields are indexed from
1
, so use$2
for second column$0
contains entire input record
- After modifying, you need to print the record. Default action is printing contents of
$0
if condition is true.1
is used idiomatically for such cases
To modify the given code in question
$ awk 'NR>1$3=$2-p p=$2 1' file
A 1
A 2 1
A 4 2
A 6 2
- Fields are indexed from
1
, so use$2
for second column$0
contains entire input record
- After modifying, you need to print the record. Default action is printing contents of
$0
if condition is true.1
is used idiomatically for such cases
answered Oct 3 '17 at 4:14
Sundeep
6,9611826
6,9611826
Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
â Johnny Tam
Oct 4 '17 at 4:14
@JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need?awk 'NR>1$3=int($2/p) p=$2 1'
â Sundeep
Oct 4 '17 at 4:58
Please go here: unix.stackexchange.com/questions/396195/⦠for a simplified illustration. :)
â Johnny Tam
Oct 5 '17 at 6:13
add a comment |Â
Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
â Johnny Tam
Oct 4 '17 at 4:14
@JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need?awk 'NR>1$3=int($2/p) p=$2 1'
â Sundeep
Oct 4 '17 at 4:58
Please go here: unix.stackexchange.com/questions/396195/⦠for a simplified illustration. :)
â Johnny Tam
Oct 5 '17 at 6:13
Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
â Johnny Tam
Oct 4 '17 at 4:14
Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
â Johnny Tam
Oct 4 '17 at 4:14
@JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need?
awk 'NR>1$3=int($2/p) p=$2 1'
â Sundeep
Oct 4 '17 at 4:58
@JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need?
awk 'NR>1$3=int($2/p) p=$2 1'
â Sundeep
Oct 4 '17 at 4:58
Please go here: unix.stackexchange.com/questions/396195/⦠for a simplified illustration. :)
â Johnny Tam
Oct 5 '17 at 6:13
Please go here: unix.stackexchange.com/questions/396195/⦠for a simplified illustration. :)
â Johnny Tam
Oct 5 '17 at 6:13
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%2f395746%2fprinting-the-difference-between-adjacent-values-in-a-column-to-a-new-column%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