0

Everyone, I have created a script for getting time difference between two different epoch time value.

Currently my script reads two value named a and b and gives the output of time different as shown below,

Input:

Enter the TIME A
12345567787

Enter the TIME B
12356777897

Output:

-00.36 hours

How I can read A and B value from a single text file named time.txt and save the time different output to another file "timediff.txt".

How my input file "time.txt" looks like.

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

What i'm expecting to output file timediff.txt looks like,

 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
1
  • Any tutorial on shell scripting should show how to read from a file. Commented Oct 4, 2017 at 20:51

2 Answers 2

1

Use a while loop that uses the read built-in. Redirect the input from the input file, and the output to the output file.

while read a b
do
    # your code here
done < time.txt > timediff.txt
Sign up to request clarification or add additional context in comments.

2 Comments

Configuring a System Event: To set SecureSphere to send syslog messages based on the CEF standard when a system event occurs: 1 Define a new Action Set and configure the parameters as follows: Name: The action set name, for example, system_syslog. Syslog Host: The IP or host name of the Syslog server. Syslog Log Level: The Syslog log level. Message: The CEF message for a system event. CEF:0|ImpervaInc.|SecureSphere|6.0|${Event.eventType}|${Event.message}|${Event.severity.displayName}|suser=${Event.username} rt=# (${Event.createTime}) cat=SystemEvent
@PrabakaranVenkatesh I don't understand what your comment has to do with my answer.
0

It would be much cleaner if your script were less verbose and took its operands from arguments rather than reading them from stdin, but (assuming your prompts are going to stderr) you can do:

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

If your prompts are intermingled with your ouptut, I strongly suggest you change your script (conveniently called myscript above) to have a more reasonable usage.

1 Comment

Since the first argument of printf is the format string, you are susceptible to variables containing the % character. Safer is printf "%s\n%s\n" "$a" "$b"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.