Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-18-2007, 09:15 AM   #1
LQ Newbie
Registered: Oct 2007
Posts: 1

Rep: Reputation: 0
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...
Old 10-18-2007, 10:02 AM   #2
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 37
In case awk is acceptable:

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

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;;

Last edited by radoulov; 10-18-2007 at 10:27 AM. Reason: adjusted numeric comparison
Old 10-18-2007, 11:37 AM   #3
LQ Newbie
Registered: Oct 2007
Location: Warsaw, Poland
Distribution: Debian
Posts: 7

Rep: Reputation: 0

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

Old 10-19-2007, 08:45 AM   #4
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
NUMS=( $(grep  -o "[0-9]\+" temp.log) )
if [ "${NUMS[(( ${#NUMS[@]} -2 ))]}" != "${NUMS[(( ${#NUMS[@]} -1 ))]}" ];then
  echo "They are different"
  echo "They are the same"
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.)
Old 10-19-2007, 07:39 PM   #5
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Originally Posted by ktgajowniczek View Post
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

may i suggest you organize your code in a more readable fashion in future. thanks
Old 09-10-2012, 09:46 AM   #6
LQ Newbie
Registered: Sep 2012
Posts: 5

Rep: Reputation: Disabled
Bush script compare numbers

Please help me.
I have file which contains
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.
Old 09-11-2012, 12:10 AM   #7
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.

Last edited by David the H.; 09-11-2012 at 12:12 AM. Reason: changed link


bash, file, shell, text

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
Bash script to compare two files swatward Linux - Newbie 17 04-18-2012 06:14 PM
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 05:48 PM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
Script to compare numbers inside two text files bugg_deccan Programming 3 10-17-2007 09:53 PM
Using diff to compare file with common lines, but at different line numbers jimieee Linux - Newbie 3 05-10-2004 07:26 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:49 AM.

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