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.

有帮助吗?

解决方案

Using awk:

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

其他提示

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'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top