Question

I am working with some data in the bash shell and need to manipulate it using sed/awk, etc and need to subtract even rows from the odd rows.

For instance:

51202598
51204294
51370060
51371733
51535314
51536526

would become:

1696
1673
1212

Thanks in advance.

Was it helpful?

Solution

Using awk:

awk 'NR%2{p=$1;next}{print ($1-p)}' file
1696
1673
1212

OTHER TIPS

Just for the fun of not using awk:

cat calc.data | paste - - | xargs -I{} echo {} "- _1 * p" | dc

or even shorter:

xargs -a calc.data -l2 printf "%s %s - _1 * p" | dc

with sed and bc (via comment by glenn):

sed -n 'h;n;G;s,\n,-,;p' calc.data|bc

or with dc alone (but in reverse order):

dc -f calc.data -e '[-_1*pswz1<r]srz1<r'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top