Reputation: 13
I'm having trouble figuring out if the current line number is greater then the next row then it should print something like for example " the number 53 is greater than 23 " and then compares the next two lines "the number 54 is less than 76". I thinking something along the lines NR%2, but not sure what to do after that. Any hints or suggestions on how this would be done would be greatly appreciated thanks.
An example of this file is:
53
23
54
76
12
42
Expected outcome
the number 53 is greater than 23
the number 54 is less than 76
the number 12 is less than 42
Upvotes: 1
Views: 122
Reputation: 47099
Just for fun here is one way of doing it with coreutils, bc and sed:
<infile paste -d' ' - - <( <infile paste -d'<' - - | bc ) |
sed 's/1$/less/; s/0$/greater/; s/([0-9]+) ([0-9]+) (.*)/the number \1 is \3 than \2/'
Output:
the number 53 is greater than 23
the number 54 is less than 76
the number 12 is less than 42
Explanation
The inner paste pipes n1<n2
to bc with returns a binary vector. The outer paste columnates this vector with pairs of numbers from the input. sed reorganizes its input based on the binary vector.
So if you were only interested in knowing if pairs of lines are greater or less than each other this bit would be enough:
<infile paste -d'<' - - | bc
Output:
0
1
1
Upvotes: 0
Reputation: 195079
this would be what you want:
awk '!(NR%2){print p>=$0?p">="$0:p"<"$0;next}{p=$0}' file
output:
53>=23
54<76
12<42
output with your new input file:
53>=23
54<76
12<42
43>=4
1<63
34<56
you can adjust the text ("greater/less than"). also handle the ==
case if you want.
Upvotes: 3