linux - 我想读取第一行值并从第二行值中减去它并继续直到结束

标签 linux bash shell unix sh

我正在寻找 shell 脚本,它可以帮助我从 txt 文件中读取数据,如下所示:。

name 1
info 21
city 28
pin  31
state 34
   -   39 

所以我需要o/p来读取第二列并写出它们之间的差异。

喜欢:

20
7
3
3
5

并在其后附加列名称,因此o/P将类似于:(“,l-”对于所有行都是常数)

20,l-name
7,l-info
3,l-city
3,l-pin
5,l-state

我已经完成了类似的代码

#!/bin/sh                                                                                                                                                                            


cat ip.txt | awk '{ print $3 }' > op.txt                                                                                                                                             
count= wc -l < op.txt                                                                                                                                                                
echo $count                                                                                                                                                                          
a=1                                                                                                                                                                                  
b=2                                                                                                                                                                                  
while read name                                                                                                                                                                      
do                                                                                                                                                                                   
if [$count -gt $a]  then                                                                                                                                                             
m=sed -n '$a' op.txt                                                                                                                                                                 
n=sed -n '$b' op.txt                                                                                                                                                                 
c=$n-$m                                                                                                                                                                              
echo $c                                                                                                                                                                              
$a=$a+1                                                                                                                                                                              
$b=$b+1                                                                                                                                                                              
fi                                                                                                                                                                                   
done<op.txt

-- 我试图找到两个值之间的差异。

最佳答案

给我们提供一个“仔细观察”像您的实际数据而不是准确表示您的数据的示例并不是特别有帮助,但要从给定的输入生成演示的输出,您可以执行以下操作:

awk 'NR>1{ printf "%d,l-%s\n", $2 - val, label } {label=$1; val=$2}' input

关于linux - 我想读取第一行值并从第二行值中减去它并继续直到结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44247737/

相关文章:

linux - 系统 cron 未运行计划执行的预期进程

c - 如何用字符串在内存中插入空字节

linux - Jetson TK1 上的 vanillaXarm 错误,XiApi 安装

linux - 如何使用 Linux 命令 Sort 根据第 4 列,数字顺序对文本文件进行排序?

linux - BASH 脚本中的错误退出不起作用

php - 当解释器位于非标准位置时,如何解决 "bad interpreter"错误?

linux - 使用单个 Linux 管道查找和更改密码

java - Android Studio 无法在 OpenSUSE 上启动(打开的文件太多)

linux - 使用 bash 打印数组中最大值的索引

Bash shell 脚本 : how do I exit and restart the script?