LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-18-2007, 09:15 AM   #1
leopard86
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
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
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

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

Rep: Reputation: 0
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

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

Rep: Reputation: 46
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.)
 
Old 10-19-2007, 07:39 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
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

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

Rep: Reputation: Disabled
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.
 
Old 09-11-2012, 12:10 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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
 
  


Reply

Tags
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


All times are GMT -5. The time now is 08:26 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration