LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-16-2011, 09:52 AM   #1
jkeertir
Member
 
Registered: Mar 2008
Posts: 70

Rep: Reputation: 15
preserving last read value in shell script


Dear ALL,

Here are the contents of the file
1600
1900
2200
2800

I want to read file content using shell script
and check if the difference between the two line count is 300

With regards,
Keerti
 
Old 02-16-2011, 10:54 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

Code:
#!/bin/bash

PrevValue="-1"

for ThisValue in $(cat infile)
do
  if [ ! $PrevValue = "-1" ]
  then
    let SubtValue=ThisValue-PrevValue
    if (( SubtValue == 300 ))
    then
      echo "difference is 300 ($ThisValue - $PrevValue)"
    else
      echo "difference is not 300 ($ThisValue - $PrevValue)"
    fi
  fi
  let PrevValue=ThisValue
done
Hope this helps.
 
1 members found this post helpful.
Old 02-16-2011, 11:35 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,563

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Or another option:
Code:
awk 'NR > 1{printf "Difference is %s300\n",($0 - x == 300)?"":"not "}{x = $0}' file
 
Old 02-16-2011, 11:52 AM   #4
jkeertir
Member
 
Registered: Mar 2008
Posts: 70

Original Poster
Rep: Reputation: 15
Smile Thanks

Thanks druuna,it worked for me
 
Old 02-16-2011, 11:53 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
You're welcome
 
Old 02-16-2011, 12:05 PM   #6
rdgreenlaw
Member
 
Registered: May 2007
Location: Newport, Maine, USA
Distribution: Debian 8.7
Posts: 72

Rep: Reputation: 18
I created a script named readfile and a test data file named testdata. Testdata contains all the data in your sample.

Code:
#!/bin/bash
read var1
echo "variable 1 is: $var1"
let var3=0
echo "Variable 3 is: $var3"
let check=0

while read var2 ; do
  echo "Variable 2 is: $var2"
  var3=$(expr $var2 - $var1)
  echo "Variable 3 is computed as var2 - var1: $var3"
  let var1=var2
  echo "Variable 1 now set to value of variable 2"
  echo "Variable 1 is $var1 Variable 2 is $var2 Variable 3 is $var3"
  echo "loop for more records"
done

echo "Read completed"
Note: I don't do testing to verify the data, I'm assuming you know how to do that. This script simply shows the process of reading the data and uses echo to display messages and values that demonstrate the process the loop is going through.

Hope this helps!

Roger
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Read a value from a file in shell script kushalkoolwal Programming 10 03-06-2012 06:22 PM
read from stdin in a shell script bujecas Linux - General 3 07-03-2009 05:46 PM
[Shell] Read a File from script yussef Programming 4 08-19-2008 05:26 AM
Shell Script -- read problem!! rossi143 Programming 4 04-02-2007 04:17 AM
shell script read non-interactive comtmr Linux - General 6 11-01-2006 07:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration