#!/bin/bash
#Create an array
mapfile -t lines < inputFile
output=()
for index in "${!lines[@]}"; do
# Check if the index + 2 exist
if [[ ${lines[$(expr $index + 2)]} ]]; then
#It does exist, do the math
output+=("$(expr ${lines[$index]} + ${lines[$(expr $index + 2)]})")
fi
done
printf "%s\n" "${output[@]}" > output
0
有了awk,我会写
awk -v ORS="" '
{a=b; b=c; c=$0} # remember the last 3 lines
NR >= 3 {print sep c-a; sep=","} # print the difference
END {print "\n"} # optional, add a trailing newline.
' file
4 回答
perly狗
创建数组如果存在两行之前的键,则打印该行减去数组中的值 .
对于像肯特回答的要求一样
又快又脏:
更新:想出了另一个有趣的方式:
update2,将结果设为CSV:
有了awk,我会写
或者让粘贴做gruntwork