![]() |
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... |
In case awk is acceptable:
Code:
awk '/Hello I return/{v=$NF;next}Code:
while IFS= read -r;do |
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 |
Code:
#!/bin/bashThe 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.) |
Quote:
|
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. |
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 12:09 PM. |