LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script to compare numbers in a txt file (http://www.linuxquestions.org/questions/programming-9/bash-script-to-compare-numbers-in-a-txt-file-592744/)

leopard86 10-18-2007 09:15 AM

Bash script to compare numbers in a txt file
 
Hi everybody, I've only a basic preparation on shell and I urgently need to have a script to do this:
I have a file with some lines written in log.txt:

Breakpoint 1 at main()...
line 10 printf("some C code"...
Hello I return 23
Program exited with code 025


Now the script must read the last two number (in this case 23 and 25) and tell me wether they are different or not...
I really need some hints, and if possible some peaces of code instead of a general explanation...
Good bye...

radoulov 10-18-2007 10:02 AM

In case awk is acceptable:

Code:

awk '/Hello I return/{v=$NF;next}
$NF+0!=v{print "They are different ...";exit}' filename

with bash it would be:

Code:

while IFS= read -r;do
        case $REPLY in
                ("Hello I return"*)v="${REPLY##* }";;
                ("Program exited with code"*)[ "${REPLY##* }" -ne "$v" ]\
                &&echo "They are different";break;;
        esac
done<filename


ktgajowniczek 10-18-2007 11:37 AM

if you don't like awk you can also do it like this:

FILENAME=log.txt; if [ $(( `tail -n2 $FILENAME | sed "s/.*\ 0*\([0-9]*\)/\1/" | paste -s | sed "s/\([0-9]*\)[^0-9]\([0-9]*\)/\1-\2/"` )) -eq 0 ]; then echo "equal codes"; else echo "not equal codes"; fi

--
KTG

/bin/bash 10-19-2007 08:45 AM

Code:

#!/bin/bash
NUMS=( $(grep  -o "[0-9]\+" temp.log) )
if [ "${NUMS[(( ${#NUMS[@]} -2 ))]}" != "${NUMS[(( ${#NUMS[@]} -1 ))]}" ];then
  echo "They are different"
else
  echo "They are the same"
fi

The first line puts all numbers in the NUMS array.
The test checks the last 2 numbers to determine if they are the same.
NOTE: This (( ${#NUMS[@]} -2 )) means Number of elements in array NUMS - 2.
Remember array elements start a 0 so the last array element is actually (Number of elements in array - 1.)

ghostdog74 10-19-2007 07:39 PM

Quote:

Originally Posted by ktgajowniczek (Post 2928607)
if you don't like awk you can also do it like this:

FILENAME=log.txt; if [ $(( `tail -n2 $FILENAME | sed "s/.*\ 0*\([0-9]*\)/\1/" | paste -s | sed "s/\([0-9]*\)[^0-9]\([0-9]*\)/\1-\2/"` )) -eq 0 ]; then echo "equal codes"; else echo "not equal codes"; fi

--
KTG

may i suggest you organize your code in a more readable fashion in future. thanks

hayko 09-10-2012 09:46 AM

Bush script compare numbers
 
Hi
Please help me.
I have file which contains
60
59
52
45
43
40
70
69
62
which bash script can give me "pass on every line and every time remembering previous number give me the first mutch where next number greater than previous, in this example 70"
Please help thank you.

David the H. 09-11-2012 12:10 AM

Please don't reopen old threads in order to ask new questions. Start a new thread instead, and link back to the old one if necessary. Only post to old threads if you have something substantial to add to that conversation.

Also, what you're asking is a trivial scripting problem and sounds very much like a homework question. The LQ rules point out that we are not obliged to give you any direct help there.

But I will say that all it requires is a simple loop where you save the value at the end of each iteration for comparison in the next step.


All times are GMT -5. The time now is 01:16 PM.