2017-10-04 91 views
0

大家,我已经创建了一个获得两个不同时代的时间值之间的时间差的脚本。从文件中读取输入和输出保存到另一个文件中的shell脚本

目前我的脚本读取名为A,B两组值,并给出不同的时间输出,如下图所示,

输入:

Enter the TIME A 
12345567787 

Enter the TIME B 
12356777897 

输出:

-00.36 hours 

如何从名为time.txt的单个文本文件中读取A和B值并保存ti我不同的输出到另一个文件“timediff.txt”。

我的输入文件“time.txt”的外观如何。

A    B 
123456789  123456755 
123445567  123434657 
128765809  141536478 
127576589  163636376 
125364758  132653758 
.    . 
.    . 
.    . 
n    n 

什么,我期待的输出文件timediff.txt样子,

A    B     output 
123456789 123456755 03.00 
123445567 123434657 09.00 
128765809 141536478 04:44 
127576589 163636376 08:22 
125364758 132653758 05:13 
.   .   . 
.   .   . 
.   .   . 
n   n   n 
+0

有关shell脚本的任何教程都应该显示如何从文件读取数据。 – Barmar

回答

1

使用while循环使用的read内置。重定向来自输入文件的输入,并将输出重定向到输出文件。

while read a b 
do 
    # your code here 
done <time.txt> timediff.txt 
0

这将是更清洁的,如果你的脚本是更简洁,并采取了从争论它的操作数,而不是从标准输入读取它们,但(假设你的提示要标准错误),你可以这样做:

while read a b; do 
    printf "$a\t$b\t%s\n" "$(printf "$a\n$b\n" | myscript)" 
done <time.txt> timediff.txt 

如果您的提示与您的ouptut混合在一起,我强烈建议您更改您的脚本(以上方便地称为myscript)以具有更合理的用法。

+0

由于printf的第一个参数是格式字符串,因此您容易受包含'%'字符的变量的影响。更安全的是'printf'%s \ n%s \ n“”$ a“”$ b“' –

相关问题